Dot*_*ght 2 javascript node.js
我无法理解如何将单个Promise调整为一系列Promise,一旦两个API调用都返回,它就会解析.
如何将下面的代码重写为Promises链?
function parseTweet(tweet) {
indico.sentimentHQ(tweet)
.then(function(res) {
tweetObj.sentiment = res;
}).catch(function(err) {
console.warn(err);
});
indico.organizations(tweet)
.then(function(res) {
tweetObj.organization = res[0].text;
tweetObj.confidence = res[0].confidence;
}).catch(function(err) {
console.warn(err);
});
}Run Code Online (Sandbox Code Playgroud)
谢谢.
小智 5
如果您希望调用同时运行,那么您可以使用Promise.all.
Promise.all([indico.sentimentHQ(tweet), indico.organizations(tweet)])
.then(values => {
// handle responses here, will be called when both calls are successful
// values will be an array of responses [sentimentHQResponse, organizationsResponse]
})
.catch(err => {
// if either of the calls reject the catch will be triggered
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |