如何使用 Sails return Promise 中的两个对象?
How to return two Objects in a Promise using Sails?
我正在使用 SailsJs v0.11.3,我想使用 Promises 来避免使用嵌套回调,但我不知道如何从一个阶段 return 两个对象(.then()
) 到另一个。
我的代码看起来像这样,但是当我尝试在第三个 .then
语句中打印 start
参数时我得到了未定义。
User.findOneById(userId)
.then(function (result) { // #1
return result;
}).then( function (result_a) { // #2
console.log('---------------- ' + result_a );
var result_b = User.findOneById(ownerId)
.then(function (result) {
console.log(result);
return result;
});
return [result_a, result_b];
}).then( function (start, finish) { // #3
console.log('***********************', start, finish);
// do something when is done
}).catch( function (err) {
// do something when is error
console.log(err);
res.json(err.status,
{
error: err
});
})
.done(function () {
res.json(200);
});
控制台结果:
---------------- [object Object]
*********************** [ { person_id: '567ab10de51dcf11001b066a',
email: 'jvilas1993@gmail.com',
permission_level: 3,
reset_password_token: null,
belongs_to: '0',
signin_count: 3,
status_active: true,
last_signin_at: '2016-01-05T17:55:49.595Z',
last_signin_ip: '0.0.0.0',
encrypted_password: 'a$tDSUsXm7nn2p4r8FczpCrubbTig/7hJmy3W5u5hrcmMHAiVBXYZ/C',
id: '567ab10de51dcf11001b0669' },
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_progressHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_settledValue: undefined } ] undefined
{ person_id: '565f57b7cd451a110063eef9',
email: 'rswomar@gmail.com',
permission_level: 1,
reset_password_token: '353635663537623763643435316131313030363365656638',
belongs_to: '0',
signin_count: 1,
status_active: false,
last_signin_at: '2015-12-02T20:42:30.434Z',
last_signin_ip: '0.0.0.0',
encrypted_password: 'aEHaE6oED65gFHqh9sE0OBOTeD.txl8DTNYLxJwgws6NIwNQ73Ky',
id: '565f57b7cd451a110063eef8' }
所以,我想知道如何在 秒 .then
[= 中 return 两个对象或变量36=]到第三个.then
。我的意思是第一个查询的结果和第二个查询的另一个结果,并在 第三个 .then
?
我希望能以正确的方式解释我的问题。
提前致谢!
使用 bluebird 的 Promise.join() and Promise.spread() 方法
User.findOneById(userId)
.then(function (result) { // #1
return result;
}).then( function (result_a) { // #2
console.log('---------------- ' + result_a );
var result_b = User.findOneById(ownerId)
.then(function (result) {
console.log(result);
return result;
});
return Promise.all([result_a, result_b]);
}).spread( function (start, finish) { // #3
console.log('***********************', start, finish);
// do something when is done
}).catch( function (err) {
// do something when is error
console.log(err);
res.json(err.status,
{
error: err
});
})
.done(function () {
res.json(200);
});
我正在使用 SailsJs v0.11.3,我想使用 Promises 来避免使用嵌套回调,但我不知道如何从一个阶段 return 两个对象(.then()
) 到另一个。
我的代码看起来像这样,但是当我尝试在第三个 .then
语句中打印 start
参数时我得到了未定义。
User.findOneById(userId)
.then(function (result) { // #1
return result;
}).then( function (result_a) { // #2
console.log('---------------- ' + result_a );
var result_b = User.findOneById(ownerId)
.then(function (result) {
console.log(result);
return result;
});
return [result_a, result_b];
}).then( function (start, finish) { // #3
console.log('***********************', start, finish);
// do something when is done
}).catch( function (err) {
// do something when is error
console.log(err);
res.json(err.status,
{
error: err
});
})
.done(function () {
res.json(200);
});
控制台结果:
---------------- [object Object]
*********************** [ { person_id: '567ab10de51dcf11001b066a',
email: 'jvilas1993@gmail.com',
permission_level: 3,
reset_password_token: null,
belongs_to: '0',
signin_count: 3,
status_active: true,
last_signin_at: '2016-01-05T17:55:49.595Z',
last_signin_ip: '0.0.0.0',
encrypted_password: 'a$tDSUsXm7nn2p4r8FczpCrubbTig/7hJmy3W5u5hrcmMHAiVBXYZ/C',
id: '567ab10de51dcf11001b0669' },
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_progressHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_settledValue: undefined } ] undefined
{ person_id: '565f57b7cd451a110063eef9',
email: 'rswomar@gmail.com',
permission_level: 1,
reset_password_token: '353635663537623763643435316131313030363365656638',
belongs_to: '0',
signin_count: 1,
status_active: false,
last_signin_at: '2015-12-02T20:42:30.434Z',
last_signin_ip: '0.0.0.0',
encrypted_password: 'aEHaE6oED65gFHqh9sE0OBOTeD.txl8DTNYLxJwgws6NIwNQ73Ky',
id: '565f57b7cd451a110063eef8' }
所以,我想知道如何在 秒 .then
[= 中 return 两个对象或变量36=]到第三个.then
。我的意思是第一个查询的结果和第二个查询的另一个结果,并在 第三个 .then
?
我希望能以正确的方式解释我的问题。 提前致谢!
使用 bluebird 的 Promise.join() and Promise.spread() 方法
User.findOneById(userId)
.then(function (result) { // #1
return result;
}).then( function (result_a) { // #2
console.log('---------------- ' + result_a );
var result_b = User.findOneById(ownerId)
.then(function (result) {
console.log(result);
return result;
});
return Promise.all([result_a, result_b]);
}).spread( function (start, finish) { // #3
console.log('***********************', start, finish);
// do something when is done
}).catch( function (err) {
// do something when is error
console.log(err);
res.json(err.status,
{
error: err
});
})
.done(function () {
res.json(200);
});