Concurrency & Workers — Series 3

Preview — 3 of 10 questions

What is logged?

javascript
const thenable = {
  then(resolve) {
    resolve('thenable');
  },
};

Promise.all([1, thenable, Promise.resolve('p')]).then((r) => console.log(JSON.stringify(r)));
A["p"]
B[1,"thenable","p"]
C[1,{},"p"]
DTypeError: thenable is not a promise

What is logged, in order?

javascript
async function main() {
  try {
    throw new Error('first');
  } catch (e) {
    throw new Error('second from catch');
  } finally {
    console.log('cleanup');
  }
}

main().catch((e) => console.log('outer:', e.message));
Aouter: first then cleanup
Bcleanup then outer: first
Ccleanup only — the second error is swallowed
Dcleanup then outer: second from catch

What is logged, in order?

javascript
const delay = (ms, v) => new Promise((r) => setTimeout(() => r(v), ms));
const timeout = (ms) => new Promise((_, rej) => setTimeout(() => rej(new Error('timeout')), ms));

let settled = false;
const work = delay(50, 'data').then((v) => {
  settled = true;
  return v;
});

Promise.race([work, timeout(10)])
  .then((v) => console.log('ok', v))
  .catch((e) => console.log('failed:', e.message));

setTimeout(() => console.log('work still ran:', settled), 80);
Afailed: timeout, then work still ran: true
Bfailed: timeout, then work still ran: false
Cok data, then work still ran: true
Dfailed: timeout only

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.