转换Javascript回调,可以多次调用promise

时间:2015-08-10 09:05:50

标签: javascript promise bluebird

有没有办法将带有回调的Javascript调用转换成可以多次调用到promise中?

说,

bcp TABLENAME out TABLENAME.csv -U username -P password -S server -c -t','

并将其包含在承诺中?

scan(function(result) {
  // this is actually a Bluetooth device scan (Cordova), and 
  // will return something when a device is found.
  // So this can be called more than once.
});

我还需要这种模式来订阅来自蓝牙设备的数据。承诺不适合这项任务吗?

编辑:我正在使用Bluebird。

1 个答案:

答案 0 :(得分:2)

承诺只解决一次。如果想要多次解决它,那么你需要的不是诺言。

您可能需要自定义事件(在浏览器中),节点EventEmitterStream(继承EventEmitter.pipe,可选缓冲)。如果您只想要一个回调链解决方案,那么编写自己的解决方案似乎并不难。

相关问题