Hard
How to Stop Async Function Execution After First Promise Error in JavaScript
How would you ensure that an asynchronous function execution stops after the first error encountered in a sequence of promises?
1const fetchData = async (id) => {2 if (id === 3) throw new Error("Invalid ID");3 return `Data for ID: ${id}`;4};56const ids = [1, 2, 3, 4];78const processIds = async () => {9 for (const id of ids) {10 console.log(await fetchData(id));11 }12};1314processIds();