0

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>
srgbnd
  • 5,404
  • 9
  • 44
  • 80
  • depending on how you were planning on promisifying it, there's different workarounds. What were you going to do to promisify it? – TKoL Nov 07 '17 at 15:12
  • This is likely a bug, I think you opened an issue for it on GitHub, hopefully will get around to it. – marknadal Dec 01 '17 at 04:41

1 Answers1

1

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.

TKoL
  • 13,158
  • 3
  • 39
  • 73