Skip to content
FrontDepth, Home

Back to the topic

Practice: Promises and async/await

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

4 predict-output · 3 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    A value and then an error travel down a promise chain. What does this code log?

    JavaScript
    Promise.resolve(1)
      .then((x) => x + 1)
      .then((x) => {
        throw new Error("boom " + x);
      })
      .then(() => console.log("skipped"))
      .catch((error) => {
        console.log(error.message);
        return 10;
      })
      .finally(() => console.log("finally"))
      .then((x) => console.log(x));
  2. Open questionJunior

    What is a promise, what states can it be in, and what does then return?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    A promise executor, a then callback and an async function with await. In what order are the letters logged?

    JavaScript
    console.log("A");
    
    const p = new Promise((resolve) => {
      console.log("B");
      resolve("C");
      console.log("D");
    });
    
    p.then((value) => console.log(value));
    
    async function run() {
      console.log("E");
      await p;
      console.log("F");
    }
    
    run().then(() => console.log("G"));
    console.log("H");
  4. Predict the outputMiddle

    The same kind of inputs go to Promise.all, allSettled, any and race. What does this code log?

    JavaScript
    const ok = (value) => Promise.resolve(value);
    const fail = (message) => Promise.reject(new Error(message));
    
    const all = await Promise.all([ok(1), fail("a"), fail("b")]).catch(
      (error) => "all: " + error.message,
    );
    console.log(all);
    
    const settled = await Promise.allSettled([ok(1), fail("a")]);
    console.log(settled.map((r) => r.status).join(", "));
    console.log(settled[1].reason.message);
    
    const any = await Promise.any([fail("x"), ok(2)]);
    console.log(any);
    
    const race = await Promise.race([fail("first"), ok(3)]).catch(
      (error) => "race: " + error.message,
    );
    console.log(race);
  5. Open questionMiddle

    A page awaits three independent requests one after another. How do you make it faster, and which Promise combinator do you choose?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Predict the outputSenior

    One then callback returns a value, another returns a promise. In what order do the lines appear?

    JavaScript
    const log = (s) => console.log(s);
    
    Promise.resolve()
      .then(() => {
        log("a1");
        return Promise.resolve("a2");
      })
      .then(log);
    
    Promise.resolve()
      .then(() => log("b1"))
      .then(() => log("b2"))
      .then(() => log("b3"))
      .then(() => log("b4"));