我使用co
执行带有一堆http请求的生成器:
co(function *(){
// resolve multiple promises in parallel
var a = httpRequest(...);
var b = httpRequest(...);
var c = httpRequest(...);
var res = yield [a, b, c];
console.log(res);
}).catch(onerror);
我有办法在每个http请求之间引入第二个睡眠吗?感谢。
答案 0 :(得分:0)
是的,你可以。每次收益 - 用延迟返回新的承诺(承诺 - 你必须解决或拒绝根据httpRequest回调)
试试这种方式
co(function *(){
var a = yield new Promise(function(resolve, reject){
setTimeout(function () {
//rosolve or reject this promise in callback of httpRequest();
}, 1000)
});
var b = yield new Promise(function(resolve, reject){
setTimeout(function () {
//rosolve or reject this promise in callback of httpRequest();
}, 1000)
});
var c = yield new Promise(function(resolve, reject){
setTimeout(function () {
//rosolve or reject this promise in callback of httpRequest();
}, 1000)
});
var res = [a, b, c];
console.log(res);
}).catch(onerror);