蓝鸟承诺绑定链

我使用蓝鸟承诺,并试图允许链调用,但使用.bind()似乎并不工作。 我正进入(状态:

TypeError:sample.testFirst(…)。testSecond不是函数

第一种方法是正确调用,并启动承诺链,但我还没有能够得到实例绑定工作。

这是我的testing代码:

var Promise = require('bluebird'); SampleObject = function() { this._ready = this.ready(); }; SampleObject.prototype.ready = function() { return new Promise(function(resolve) { resolve(); }).bind(this); } SampleObject.prototype.testFirst = function() { return this._ready.then(function() { console.log('test_first'); }); } SampleObject.prototype.testSecond = function() { return this._ready.then(function() { console.log('test_second'); }); } var sample = new SampleObject(); sample.testFirst().testSecond().then(function() { console.log('done'); }); 

我正在使用最新的蓝鸟通过:

npm安装 – 保存蓝鸟

我接近这个错误? 我将不胜感激任何帮助。 谢谢。

这是抛出这个错误,因为没有方法testSecondtestFirst ,如果你想在testFirst解决后做一些事情,像下面这样做:

 var sample = new SampleObject(); Promise.join(sample.testFirst(), sample.testSecond()).spread(function (testFirst, testSecond){ // Here testFirst is returned by resolving the promise created by `sample.testFirst` and // testSecond is returned by resolving the promise created by `sample.testSecond` }); 

如果你想检查两者是否正确解决,而不是做一个console.log,返回testFirsttestSecond函数中的string,并将其logging在spreadcallback中,如下所示:

 SampleObject.prototype.testFirst = function() { return this._ready.then(function() { return 'test_first'; }); } SampleObject.prototype.testSecond = function() { return this._ready.then(function() { return 'test_second'; }); } 

现在,在spreadcallback中做一个console.log ,如下所示,将logging上面承诺返回的string:

 Promise.join(sample.testFirst(), sample.testSecond()).spread(function(first, second){ console.log(first); // test_first console.log(second); // test_second });