2017-01-26 29 views
0

我使用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

这样吗? – hackerrdave

+0

或仍然平行 - 每个延迟1秒? – hackerrdave

+0

依次,依次间隔一秒钟,理想情况下。本质上我不希望服务器端恨我。 –

回答

0

是的,你可以。每种收益 - 与dilay返回新的承诺(在承诺 - 你不得不解雇解决或拒绝acording到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);