run promise one by one
function runSerial(tasks) { var result = Promise.resolve(); tasks.forEach(task => { result = result.then(() => task()); }); return result; }
Here is what the above code is Doing:
1. We create a new Promise that is immediately resolved.
2. We loop through each task in the tasks array.
3. For each task, we chain on a new .then() handler.
4. The .then() handler calls the task function and returns its result.
5. The result of the .then() handler is a new Promise, which is passed to the next .then() handler.
6. When the loop is done, we return the result Promise.
The result of runSerial() is a Promise that resolves when all the tasks have completed.
The runSerial() function is a good example of how to use the Promise API to create a new function.
The Promise API is a powerful tool for creating new functions that work with Promises.