JSFiddle - React, Tailwind, and code Playground
by Alexey Mishin
JavaScript
const promiseMaker = url => {
const result = `Finished the ${url}`;
return new Promise(resolve => {
setTimeout(() => {
resolve(result);
}, 2000);
});
};
const urls = [
'user.url',
'guest.url',
'admin.url',
'whoKnows.url'
];
const chainPromiser = arr => {
let chain = Promise.resolve();
let results = [];
arr.forEach(el => {
chain = chain
.then(() => promiseMaker(el))
.then(result => {
console.log(result);
return result;
})
.then(result => {
results.push(result)
});
});
chain.then(() => {
console.log(results);
});
};
chainPromiser(urls);