then()中的ensureErrorObject(bluebird承诺)

时间:2015-08-01 04:34:21

标签: node.js promise bluebird requestjs

我正在尝试用bluebird开发一个函数并请求。在我的模块中:

module.prototype.getUsers = function (opts)
 {
    return new Promise(function(reject, resolve){
        request
        .post({
            url: opts.uri,
            form: {request: opts.req}
        },
        function(err, res, body){
            if(err)
            {
                reject(err);
            }
            else
            {
                resolve(body);
            }
        });

    });
 };

在实施中我有:

obj.getUsers()
.then(
    function(res){
        console.log(res);
    })
.catch(
    function(err){
        throw(err);
    });

但我明白了: 未处理拒绝错误:“请求结果”

at Object.ensureErrorObject (C:\Dev\projects\testApi\node_modules
\bluebird\js\main\util.js:261:20)
    at Promise._rejectCallback (C:\Dev\projects\testApi\node_modules\
bluebird\js\main\promise.js:465:22)
    at C:\Dev\projects\testApi\node_modules\bluebird\js\main\promise.
js:482:17
    at Request._callback (C:\Dev\projects\testApi\ebix.js:53:6)
    at Request.self.callback (C:\Dev\projects\testApi\node_modules\re
quest\request.js:198:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (C:\Dev\projects\testApi\node_modules\requ
est\request.js:1057:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (C:\Dev\projects\testApi\node_modu
les\request\request.js:1003:12)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

我做错了什么?我对承诺和蓝鸟完全不满​​意。

1 个答案:

答案 0 :(得分:1)

您混淆了Promise回调的参数:

function(reject, resolve) { ... }

应该是the other way around

function(resolve, reject) { ... {