Promise.map 没有返回正确的顺序
Promise.map not returning right order
如何使用 bluebird 完成以下工作。
groupCast = [Promise.resolve("name1"), Promise.resolve("name2"),Promise.resolve("name3")]
Promise.map( groupCast , function (group){
Promise.resolve($http.get("/getdata" , params:{group:group}))
.then(function(response){ console.log(group," done")return response}))
return response
})
.then(function(resp){console.log(resp)})
如果每个组对 http 调用的响应是 "one"、"two"、"three"
然后我们期望有:
"name1 done";
"name2 done";
"name3 done";
[ "one" , "two" ,"three" ]
但是我得到
[ "one" , "two" ,"three" ]
"name1 done";
"name2 done";
"name3 done";
我该如何解决。我不能使用异步和等待,因为 IE 不支持它。
首先,您的代码格式让您很难看清发生了什么。让我稍微清理一下并添加一些评论,以便您了解发生了什么。
Promise.map(groupCast, function(group) {
//the following kicks off a new promise, which is not chained to the current one
Promise.resolve($http.get("/getdata", { params: { group: group } })).then(
function(response) {
console.log(group, " done");
//you're returning response below, but it's not going anywhere!
return response;
}
);
//The current promise resolves right away, not waiting for the $http call.
//Also, you're returning an undefined value here.
return response;
}).then(function(resp) {
//the prior promise resolves with undefined.
console.log(resp);
});
现在让我们修复它,以便 promise 链正确。
Promise.map(groupCast, function(group) {
// $http.get returns a promise already. No need to wrap it in one.
// Also, let's go ahead and return the chained promise so the `map` function can wait for it.
return $http.get("/getdata", { params: { group: group } })
.then(function(response) {
console.log(group, " done");
return response;
});
}).then(function(resp) {
//the prior promise should now resolve as expected.
console.log(resp);
});
如何使用 bluebird 完成以下工作。
groupCast = [Promise.resolve("name1"), Promise.resolve("name2"),Promise.resolve("name3")]
Promise.map( groupCast , function (group){
Promise.resolve($http.get("/getdata" , params:{group:group}))
.then(function(response){ console.log(group," done")return response}))
return response
})
.then(function(resp){console.log(resp)})
如果每个组对 http 调用的响应是 "one"、"two"、"three" 然后我们期望有:
"name1 done";
"name2 done";
"name3 done";
[ "one" , "two" ,"three" ]
但是我得到
[ "one" , "two" ,"three" ]
"name1 done";
"name2 done";
"name3 done";
我该如何解决。我不能使用异步和等待,因为 IE 不支持它。
首先,您的代码格式让您很难看清发生了什么。让我稍微清理一下并添加一些评论,以便您了解发生了什么。
Promise.map(groupCast, function(group) {
//the following kicks off a new promise, which is not chained to the current one
Promise.resolve($http.get("/getdata", { params: { group: group } })).then(
function(response) {
console.log(group, " done");
//you're returning response below, but it's not going anywhere!
return response;
}
);
//The current promise resolves right away, not waiting for the $http call.
//Also, you're returning an undefined value here.
return response;
}).then(function(resp) {
//the prior promise resolves with undefined.
console.log(resp);
});
现在让我们修复它,以便 promise 链正确。
Promise.map(groupCast, function(group) {
// $http.get returns a promise already. No need to wrap it in one.
// Also, let's go ahead and return the chained promise so the `map` function can wait for it.
return $http.get("/getdata", { params: { group: group } })
.then(function(response) {
console.log(group, " done");
return response;
});
}).then(function(resp) {
//the prior promise should now resolve as expected.
console.log(resp);
});