JSFiddle - React, Tailwind, and code Playground
JavaScript
function log(message) {
$('#log').append(message + '<br>');
}
function chain_promises(list, fun) {
return list.reduce(
function (promise, element) {
return promise.then(function () {
// I only needed to kick off some side-effects. If you need to get a list back, you would append to it here.
fun(element);
});
},
// An initial promise just kicks things off.
Promise.resolve(true)
);
}
// Test it.
function test_function (element) {
return new Promise(function (pass, _fail) {
log('Processing ' + element);
pass(true);
});
}
chain_promises([1, 2, 3, 4, 5], test_function).then(function () {
log('Done.');
});