0

我正在尝试使用 bluebird 和 request 开发一个功能。在模块中我有:

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)

我做错了什么?我对承诺和蓝鸟完全陌生。

4

1 回答 1

1

您混淆了Promise回调的参数:

function(reject, resolve) { ... }

应该是相反的

function(resolve, reject) { ... {
于 2015-08-01T08:10:41.653 回答