如何在节点快速路由器中沿着承诺链“转移”变量?

时间:2016-03-02 10:26:53

标签: javascript node.js express promise router

我正在尝试在promise链中进行两次单独的DB调用,尽管出于测试目的,第一次调用被替换为传递的简单字符串。

我的问题是我无法在第二个承诺中访问变量msg(我尝试设置context.foo = msg。

router.route("/")
.get(function(request, response) {

    var session = request.session;

    return new Promise(function(resolve, reject) {

        resolve("h!");

    }).then(function(msg){

        return new Promise(function(resolve, reject) {

            Snippet.find({}, function(error, data) {
                let context = {
                    snippets: data.map(function(snippet) {
                        return {
                            name: snippet.name,
                            snippet: snippet.snippet,
                            createdAt: snippet.createdAt,
                            user: snippet.user,
                            id: snippet._id
                        };
                    }),
                  foo: msg
                };
                resolve(context);
            });
        });

    }).then(function(context){
        response.render("start/index", context);
    }).catch(function(err){
        response.end(err);
    });
});

另一种尝试,这里试图将路由器绑定到承诺......

 router.route("/")
.get(function(request, response) {

    var session = request.session;

    return new Promise(function(resolve, reject) {

        resolve("hi!");

    }).then(function(msg){

        router.msg = msg;

        return new Promise(function(resolve, reject) {

            Snippet.find({}, function(error, data) {
                let context = {
                    snippets: data.map(function(snippet) {
                        return {
                            name: snippet.name,
                            snippet: snippet.snippet,
                            createdAt: snippet.createdAt,
                            user: snippet.user,
                            id: snippet._id
                        };
                    }),
                  foo: this.msg
                };
                resolve(context);
            }.bind(router));
        });

第三次尝试......

router.route("/")
.get(function(request, response) {

    var session = request.session;

    return new Promise(function(resolve, reject) {

        context.msg = "hi!";

        resolve(context);

    }).then(function(context){

        return new Promise(function(resolve, reject) {
            Snippet.find({}, function(error, data) {
                context.snippets = {
                    snippets: data.map(function(snippet) {
                        return {
                            name: snippet.name,
                            snippet: snippet.snippet,
                            createdAt: snippet.createdAt,
                            user: snippet.user,
                            id: snippet._id
                        };
                    }),
                };
                resolve(context);
            });
        });

    }).then(function(context){
        response.render("start/index", context);
    }).catch(function(err){
        response.end(err);
    });
});

所以基本的问题始终是,当我没有周围的对象时,如何“注入”或使用Promise范围内的变量,没有“这个”来实现它=)

1 个答案:

答案 0 :(得分:1)

使用蓝鸟强大的 Promise.coroutine()

router.route("/")
.get(Promise.coroutine(function*(request, response) {
    try {
        const dbResult1 = yield getValueFromDatabaseAsync(/* w/e */);
        const dbResult2 = yield getValueFromDatabaseAsync(/* w/e, can be dbResult1 */);

        const context = {...}; // use dbResult1 and dbResult2 here normally
        response.render("start/index", context);
    } catch (err) {
        response.end(err);
    }
}));