InterviewHubPRO
Section Overview
Q69

Question 69 of 521

Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)

13%
JavaScript Internals (521 Qs)•Part 2: Promises, Callbacks, Async & Strict Mode (54 - 103)

Does Promise.all() cancel the other Promises?

No. Promise.all() does not cancel the other promises. It only waits for all of them to finish, and if any one rejects, the overall Promise.all() call rejects immediately.

A JavaScript promise does not have a built-in cancel API. Once a promise starts, it keeps running unless the underlying async operation supports cancellation on its own. For example, fetch() supports cancellation using AbortController.

javascript
const controller = new AbortController();
const signal = controller.signal;

const requestA = fetch("/api/a", { signal });
const requestB = Promise.reject(new Error("Server error"));
const requestC = fetch("/api/c", { signal });

Promise.all([requestA, requestB, requestC])
  .then((results) => console.log(results))
  .catch((error) => {
    console.log("Promise.all rejected:", error.message);
    controller.abort(); // Cancels the fetch requests still in progress
  });

In the example above, if requestB rejects, Promise.all() rejects, but requestA and requestC are not automatically canceled. They continue unless you explicitly abort them using the underlying API or custom logic.

How to cancel async work:

  1. Use an API that supports cancellation, such as fetch() with AbortController.
  2. For custom promises, you can add a cancel() method or a flag to stop work internally.
  3. If the work is not cancelable, the promise cannot be truly canceled from outside.

Note: Promise.all() is about aggregation, not cancellation. It waits for all promises to settle, but it does not stop the other async operations by itself.

What is the difference between return and return await in async functions

In an async function, return value simply returns the value or promise. If the value is a rejected promise, the rejection is not caught by a surrounding try/catch unless you await it first.

return await value pauses the function until the promise settles, so a try/catch around it can handle errors properly.

javascript
async function withoutAwait() {
  try {
    return Promise.reject(new Error("Something failed"));
  } catch (error) {
    console.log("This will not run");
    return "fallback";
  }
}

async function withAwait() {
  try {
    return await Promise.reject(new Error("Something failed"));
  } catch (error) {
    console.log("Caught inside the function:", error.message);
    return "fallback";
  }
}

withoutAwait().catch((err) => console.log("outside catch:", err.message));
withAwait().then((value) => console.log("withAwait result:", value));

Output:

javascript
outside catch: Something failed
Caught inside the function: Something failed
withAwait result: fallback

In short:

  • return promise returns the promise without waiting for it.
  • return await promise waits for the promise and allows the surrounding try/catch to catch errors.

return await is especially useful when you want to clean up or handle the error inside the same async function before it exits.