Execute async tasks (promises) in Sequence | JavaScript Interview Question - 68

preview_player
Показать описание
JavaScript Interview Question - 67 | In this video, we will see how to solve a medium-difficulty problem asked in a frontend engineer interview to SDE1, SDE2, and SDE3.

We have to implement a function named executeAsync in JavaScript that takes an array of async tasks (promises) and a callback function as input, executes all the promises sequentially one after the another, and then after each promise is settled invokes the callback function with the result of passed and failed promises.

You can expect this frontend system design/coding question in Rippling, Uber, Flipkart, Atlassian, Meta, Google, Microsoft, Dropbox, TCS, Infosys, Wipro, Cognizant, Capgemini, Accenture, Nvidia, Nutanix, and other product-based organizations' interviews.

Loved the question? I have 120+ solved problems in my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my E-book.

Social links
Рекомендации по теме
Комментарии
Автор

Hi Prashant, solved it on my own. Also fixed the issue due to which your solution runs in parallel.

const createAsyncTask = () => {
const value = Math.floor(Math.random() * 10)
return () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(value < 5){
reject(`Error: ${value}`)
}
resolve(value * 1000)
}, value * 1000)
})
}
}

const task1 = createAsyncTask()
const task2 = createAsyncTask()
const task3 = createAsyncTask()
const task4 = createAsyncTask()
const task5 = createAsyncTask()
const task6 = createAsyncTask()
const task7 = createAsyncTask()
const task8 = createAsyncTask()
const task9 = createAsyncTask()
const task10 = createAsyncTask()

const taskList = [task1, task2, task3, task4, task5, task6, task7, task8, task9, task10]


const asyncSequence = async (taskList, callback) => {
const results = []
const errors = []

for(let task of taskList){
try {
const promise = task()
const result = await promise
console.log(result)
results.push(result)
}catch(error){
console.log(error)
errors.push(error)
}
}

callback(results, errors)
}

asyncSequence(taskList, (results, errors) => {
console.log(errors)
console.log(results)
})

akash-kumar
Автор

hey @learnerbucket, this will execute all async functions simoltaneously, not in series. Because the taskArray will contain the results of immediately invoking async functions as you have already invoked all async function in array itself. Once we just pass ref of async functions in the array, it will work as expected

coderinprocess
Автор

Your code will still runs the promises in parallel which means they are not running sequentially.

rajanbaliwal
Автор

I do not think promise are started in series here. Since you are calling the function inside the array only, all promises are triggered there only and all are in parallel.

lalipathak-gc
Автор

this seems more like of Polyfill implementation for Promise.all. Correct me if I am wrong.

SANJEETkumar-ylkt
join shbcf.ru