DarkPhoenix,
function request(url) {
return new Promise((res, rej) => {
const delayTime = Math.floor(Math.random() * 10000) + 1;
setTimeout(() => res(url), delayTime);
});
}
function willGetUrls(urls) {
if (!urls || !urls.length) { return Promise.resolve([]); }
return new Promise(function(resolve, reject) {
var count = urls.length;
var res = [];
urls.forEach(function(url, idx) {
request(url).then(function(v) {
res[idx] = v;
if (--count < 1) { resolve(res); console.log(res)}
});
});
});
}
var urls = [
'simon',
'david',
'chak',
'richard'
];
willGetUrls(urls)