I would like to add some timeout between below get requests. I mean, flow should be looks like: timeout, https://example.com/example1, timeout, https://example.com/example2, timeout, https://example.com/example3, timeout
etc. (or without first timeout, whatever).
Below function is working properly:
function promiseGetInformationFromMultipleUrls(parts) {
return Promise.all(parts.map(part => {
return request({
'url': `https://example.com/${part}`,
'json': true
}).then(partData => {console.log("Part data was fetched: " + part); return partData.result;})
.catch(err => {console.error("Error during fetching data from part: " + part + ", error code: " + err.statusCode);});
}));
}
Where parts is -> example1, example2, example3....
I am trying do it by adding timer:
const timer = ms => new Promise( res => setTimeout(res, ms));
And use it:
function promiseGetInformationFromMultipleUrls(parts) {
return Promise.all(parts.map(part => {
console.log("wait 1 seconds");
timer(1000).then(_=>console.log("done"));
return request({
'url': `https://example.com/${part}`,
'json': true
}).then(partData => {console.log("Part data was fetched: " + part); return partData.result;})
.catch(err => {console.error("Error during fetching data from part: " + part + ", error code: " + err.statusCode);});
}));
}
But it is wrong flow -> timeout, timeout, timeout,..., get request1, get request 2, get request 3
.