I am using Bluebird for promises and trying to allow chain calling however using .bind() doesnt seem to work. I am getting:
TypeError: sample.testFirst(...).testSecond is not a function
The first method is called correctly and starts the promise chain but I havent been able to get the instance binding working at all.
This is my test code:
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');
});
I am using the latest bluebird via:
npm install --save bluebird
Am I approaching this wrong? I would appreciate any help. Thanks.
It is throwing that error because, there is no method
testSecondontestFirst, If you want to do something after both the Promises are resolved, do it like below :If you want to check if both are resolved properly, instead of doing a console.log , return the string in
testFirstandtestSecondfunctions and log them in thespreadcallback as shown below :Now, doing a
console.login thespreadcallback as shown below , will log the strings returned by the promises above :