如何使用 Sails 返回 Promise 中的两个对象? [英] How to return two Objects in a Promise using Sails?

查看:64
本文介绍了如何使用 Sails 返回 Promise 中的两个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SailsJs v0.11.3 并且我想使用 Promises 来避免使用嵌套回调,但我不知道如何从一个阶段返回两个对象 (.then()) 到另一个.

I'm using SailsJs v0.11.3 and I want to use Promises to avoid using nested callback but I don't know how to return two objects from one stage (.then()) to another one.

我的代码看起来像这样,但是当我尝试在第三个 .then 语句上打印 start 参数时我得到了未定义.

My code looks like this but I got undefined when trying to print start param on the third .then statement.

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: '$2a$10$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: '$2a$10$37EHaE6oED65gFHqh9sE0OBOTeD.txl8DTNYLxJwgws6NIwNQ73Ky',
  id: '565f57b7cd451a110063eef8' }

所以,我想知道如何将 second .then 中的两个对象或变量返回给 第三个​​.then.我的意思是第一个查询的结果和第二个查询的另一个结果,并在 第三个​​ .then 中接收这些结果?

So, I'd like to know how can I return two objects or variables in the second .then to the third .then. I mean the result from the first query and another result made on the second query and receive those in the third .then?

我希望以正确的方式解释我的问题.提前致谢!

I hope to explain my problem in the right way. Thanks in advance!

推荐答案

使用 bluebird 的 Promise.join()Promise.spread() 方法

Use bluebird's Promise.join() and Promise.spread() methods

  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);
    });

这篇关于如何使用 Sails 返回 Promise 中的两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆