如果传递 null,则不调用 Put 回调

Put callback is not called if null passed

枪 0.8.9

我想 "promisify" .put(cb) 方法并注意到如果 null 作为数据传递,回调 cb 从未被触发。

很难控制节点 selection。例如,如果删除了节点属性并且我只想 select 具有属性的节点。我不确定 .put(null) 是否删除了属性。

看下面的代码,通知dino removed的回调没有触发。

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>

我对 Gun 一无所知,但这可能是处理这种情况的一种方法:

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

Gun.pput,而不是 Gun.put,然后 return 一个承诺。