5-min frontend interview prep
Welcome to another episode of 5-min frontend interview prep
Today we will implement a function that takes a list of async functions as input and executes them in a series that is one at a time , next tasks will be executed only when previous task is completed
Example :
Input: [ AsyncTask(3), AsyncTask(1), AsyncTask(2) ] Output: 3 1 2
const asyncSeriesExecuter = function(promises) {
// get the top task
let promise = promises.shift();
//execute the task
promise.then((data) => {
//print the result
console.log(data);
//recursively call the same function
if (promises.length > 0) {
asyncSeriesExecuter(promises);
}
});
}
so basically we used recusrion to solve this , our aim was to run tasks in series , so we use shift which will take first element and we used then and we console.log it and we also set some condition it should run until the array is empty after that every tasks is completed that's what we aimed for right?
Have a great day