【发布时间】:2017-12-09 07:30:28
【问题描述】:
我是 Promise 的新手,我使用 bluebird 文档从异步代码中获取数据
我尝试的是以下内容:
错误是:
getToken.then不是函数
如何避免?
这个文件connection.js
return connection.getToken.then(function(connToken){
console.log(connToken);
}).catch({
})
这是moduleB中getToken的代码
const request = require("request-promise");
const Promise = require("bluebird");
module.exports = {
getToken: function () {
return new Promise((resolve, reject) => {
let options = {
method: 'POST',
url: 'https://authentication.arc.com/oauth/token',
headers: {
grant_type: 'client_credentials',
authorization: 'Q0MDdmMCFiMTc0fGNvlQVRDWThDNDFsdkhibGNTbz0=',
accept: 'application/json;charset=utf-8',
'content-type': 'application/x-www-form-urlencoded'
},
form: {
grant_type: 'client_credentials',
token_format: 'opaque&response_type=token'
}
};
request(options)
.then(function (body) {
return body;
})
.catch(function (err) {
return err;
});
})
}
}
【问题讨论】:
-
你有什么问题?
-
@Aron - 抱歉用错误更新问题...
-
除了明显的拼写错误(缺少方法调用),避免
Promiseconstructor antipattern和Drop the pointless.then(body => body)!
标签: javascript node.js promise bluebird