Answer the question
In order to leave comments, you need to log in
How to properly queue requests in javascript with a delay?
There is a set of http requests, how to execute them in order, with a delay?
UPD1 : At the moment such a solution is written, but there is a fear that there will be memory leaks
UPD2 : There are no memory leaks.
class Balancer {
constructor(kwArr, timeout) {
this._i = 0;
this._kwArr = kwArr;
this._tikTimeout = timeout;
this.start();
}
start() {
this._instance = setInterval( () => { this._tik(); }, this._tikTimeout );
}
stop() {
clearInterval(this._instance);
}
_tik() {
if(this._i < this._kwArr.length) {
var kw = this._kwArr[this._i++];
requestFunc(kwArr.url);
} else {
this.stop();
}
}
}
Answer the question
In order to leave comments, you need to log in
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function doRequest(urls){
var url = urls.shift();
if( ! url){
return Promise.resolve();
}
return request(url)
.then(() => {
return sleep(5000);
})
.then(() => {
return doRequest(urls);
});
}
doRequest([
'http://google.com',
'http://ya.ru'
])
.then(() => {
// complete
})
.catch(err => {
console.error(err);
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question