【发布时间】:2016-10-19 20:00:55
【问题描述】:
我在如何解决链式 Promise 并将数据传回时遇到问题。我正在使用带有请求承诺的节点 js。这是我的代码
start(name)
.then(firstFunction)
.then(secondFuntion)
.then(function(){
// i want to return data from secondfunction back
))
问题在于 secondFunction 我有一个 for 循环,它对我从 firstFunction 获得的每个对象执行调用,它是一个对象数组。我是在每次迭代后还是在所有迭代后解决承诺。创建一个全局对象并将结果保存到其中并返回该结果会更聪明吗?我的 secondFunction 代码如下所示
var secondFunction = function(data){
var promise = new Promise(function(){
for(var i= 0; i <data.length; i ++){
options = { url: "", jason: true}
rp.(options)
.then(function(resp){
// i do something with respose and need to save this
//should i resolve the promise here??
})
.catch(function(err){
});
}
});
return promise;
}
编辑
我想通了!感谢所有帮助,在我的第二个函数中,我做到了这一点
var task = function(item){
// performed the task in here
}
var actions = data.map(task);
return Promise.all(actions);
【问题讨论】:
标签: javascript angularjs node.js promise