Return a promise from a promise
Wondering how to get a promise back from a promise. For example.
I have a construction like this:
doAsyncStuff() // a promise
.then( function(res) {
doAnotherAsyncStuff(res) // another promise
.then( makeSomeThings )
.then( function(anotherRes, opts) {
...
})
})
.then( ... )
I want to write it like this:
doAsyncStuff() // a promise
.then( function(res) {
doAnotherAsyncStuff(res) // another promise
.then( makeSomeThings )
// and somehow push-out promise
})
.then( function(anotherRes) {
...
})
.then( ... )
How can I achieve this result?
problem
var Promise = require('bluebird');
//noinspection JSUnresolvedFunction
var bcrypt = Promise.promisifyAll(require('bcrypt'));
var Sequelize = require('sequelize');
var config = require('config');
var sequelize = new Sequelize(config.get('db.connstring'));
//noinspection JSUnresolvedFunction
var User = sequelize.define('user', {
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
}
},
passwordHash: {
type: Sequelize.STRING
},
isConfirmed: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
freezeTableName: true,
classMethods: {
login: Promise.method(function (email, password) {
if (!email || !password) throw new Error('Email and password are both required');
var rv = this
.find({where: {email: email.toLowerCase().trim()}})
.then(function (user) {
return bcrypt.compareAsync(password, user.passwordHash).then(function (res) {
console.log(email, password, res);
});
// if i dont use pacthed compare here, i have no problem ..
// return bcrypt.compare(password, user.passwordHash, function(err, res) {
// console.log(email, password, res);
// });
});
console.log('B', rv);
return rv;
})
}
});
sequelize.sync({force: true}).then(function () {
var pwd = 'pwd';
//noinspection JSUnresolvedFunction
bcrypt.hashAsync(pwd, 4).then(function (salt) {
var u1 = User.create({
name: 'u1',
email: 'u1@ex.com',
passwordHash: salt
}).then(function (result) {
User.login('u1@ex.com', pwd).then(function (res) {
console.log('A', res)
})
});
});
});
source to share
If the function presents a conversion of the previous (resolved) result to either the next result or a promise to be resolved to the next result, you just need to pass it directly to then
.
function return1() {
return Promise.resolve(1);
}
function wait(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}
function increment(val) {
return wait(1000).then(function () {
return val + 1;
});
}
function square(val) {
return wait(1000).then(function () {
return val * val;
});
}
var p = return1()
.then(increment)
.then(square); // Returns a promise that will resolve to 4 (eventually)
p.then(function (result) { console.log(result); });
Demo here: http://jsfiddle.net/Lzxtuu1b/
source to share
As the other answers have already said, you have to do callbacks . Whether you return an equal value or a promise on a value doesn't matter, both will work; but when you don't return anything, the resulting promise will be resolved with . return
then
undefined
In your specific examples, it is located here:
doAsyncStuff() // a promise
.then( function(res) {
return doAnotherAsyncStuff(res) // another promise
// ^^^^^^
.then( makeSomeThings )
…
…
.then(function (user) {
return bcrypt.compareAsync(password, user.passwordHash).then(function (res) {
console.log(email, password, res);
return res;
// ^^^^^^
});
In your last example, you can even disable a lot of calls when using return
:
var pwd = 'pwd';
sequelize.sync({force: true}).then(function () {
//noinspection JSUnresolvedFunction
return bcrypt.hashAsync(pwd, 4);
}).then(function (salt) {
return User.create({
name: 'u1',
email: 'u1@ex.com',
passwordHash: salt
});
}).then(function (result) {
return User.login('u1@ex.com', pwd);
}).then(function (res) {
console.log('A', res);
});
source to share