then() 中的 ensureErrorObject(蓝鸟承诺)
ensureErrorObject in then() (bluebird promises)
我正在尝试使用 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);
});
但我得到:
未处理的拒绝错误:"result of request"
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)
我做错了什么?我对 promises 和 bluebird 完全陌生。
您混淆了 Promise
回调的参数:
function(reject, resolve) { ... }
function(resolve, reject) { ... {
我正在尝试使用 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);
});
但我得到: 未处理的拒绝错误:"result of request"
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)
我做错了什么?我对 promises 和 bluebird 完全陌生。
您混淆了 Promise
回调的参数:
function(reject, resolve) { ... }
function(resolve, reject) { ... {