使用Bluebird在Promise中包装Node.js回调 [英] Wrapping Node.js callbacks in Promises using Bluebird

查看:95
本文介绍了使用Bluebird在Promise中包装Node.js回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Bluebird中使用Promise包装Node.js回调?这是我想出的,但是想知道是否有更好的方法:

How do I wrap a Node.js callback using a Promise in Bluebird? This is what I came up with, but wanted to know if there is a better way:

return new Promise(function(onFulfilled, onRejected) {
    nodeCall(function(err, res) {
            if (err) {
                onRejected(err);
            }
            onFulfilled(res);
        });
});

如果只需要返回一个错误,是否有更清洁的方法来做到这一点?

Is there a cleaner way to do this if only an error needs to be returned back?

修改 我尝试使用Promise.promisifyAll(),但结果没有传播到then子句.我的具体示例如下所示.我正在使用两个库:a)sequelize,它返回promise,b)超级测试(用于测试http请求),它使用节点样式回调.这是不使用promisifyAll的代码.它调用sequelize初始化数据库,然后发出HTTP请求以创建订单. Bosth console.log语句正确打印:

Edit I tried to use Promise.promisifyAll(), but the result is not being propagated to the then clause. My specific example is shown below. I am using two libraries: a) sequelize, which returns promises, b) supertest (used for testing http requests), which uses node style callbacks. Here's the code without using promisifyAll. It calls sequelize to initialize the database and then makes an HTTP request to create the order. Bosth console.log statements are printed correctly:

var request = require('supertest');

describe('Test', function() {
    before(function(done) {
        // Sync the database
        sequelize.sync(
        ).then(function() {
            console.log('Create an order');
            request(app)
                .post('/orders')
                .send({
                    customer: 'John Smith'
                })
                .end(function(err, res) {
                    console.log('order:', res.body);
                    done();
                });
        });
    });

    ...
});

现在,我尝试使用promisifyAll,这样我就可以用以下方式链接通话了:

Now I try to use promisifyAll, so that I can chain the calls with then:

var request = require('supertest');
Promise.promisifyAll(request.prototype);

describe('Test', function() {
    before(function(done) {
        // Sync the database
        sequelize.sync(
        ).then(function() {
            console.log('Create an order');
            request(app)
                .post('/orders')
                .send({
                    customer: 'John Smith'
                })
                .end();
        }).then(function(res) {
            console.log('order:', res.body);
            done();
        });
    });

    ...
});

当我进入第二个console.log时,res参数未定义.

When I get to the second console.log the res argument is undefined.

Create an order
Possibly unhandled TypeError: Cannot read property 'body' of undefined

我在做什么错了?

推荐答案

您没有调用promise返回版本,也没有返回它.

You are not calling the promise returning version and not returning it either.

尝试一下:

   // Add a return statement so the promise is chained
   return request(app)
            .post('/orders')
            .send({
                customer: 'John Smith'
            })
            // Call the promise returning version of .end()
            .endAsync(); 

这篇关于使用Bluebird在Promise中包装Node.js回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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