Search code examples
javascriptnode.jsgun

Put callback is not called if null passed


gun 0.8.9

I wanted to "promisify" .put(cb) method and noticed that the callback cb was never triggered if null passed as data.

It makes hard to control node selection. For example, if node properties were removed and I want to select only nodes which have properties. I'm not sure that .put(null) removed properties.

Look the code below, the callback which notifies that dino removed is not triggered.

var gun = new Gun();
var park = gun.get('park');

var velociraptor = park.get('dino/velociraptor').put({
  statistics: {
    speed: 17,
    intelligence: 21,
    force: 11
  }
}, function (ack) {
  console.log('add dino', ack);
});

park.get('dino/velociraptor').put(null, function (ack) {
  console.log('remove dino', ack);
  alert('You have removed the velociraptor from the park!');
});
<script src="https://rawgit.com/amark/gun/master/gun.js"></script>


Solution

  • I know nothing about Gun, but this would be one way of handling that situatino maybe:

    Gun.prototype.pput = function(arg){
        return new Promise((resolve, reject) => {
              this.put(arg, resolve);
              if (arg === null) resolve();
        })        
    }
    

    Gun.pput, instead of Gun.put, would then return a promise.