LearnFrenzy
Sign in Take a free test

JavaScript interview · Set 10 of 10

JavaScript Tricky Questions – Async, Closures and this

  • 6 follow-up questions
  • 4 key points
  • Code examples
Test yourself

Two rules at once

Senior output questions mix the event loop with closures, this with callbacks, or async functions with control flow. Trace step by step and name each rule.

  • AsyncNested queues, forEach and unawaited promises.
  • ScopeClosures capture variables, not values.
  • Objectsthis in callbacks, prototypes and getters.

These are the harder output questions senior front-end interviews use. Each combines two rules — for example the event loop and closures — so trace the code step by step before reading the answer.

1. What is logged? (promises inside timers and timers inside promises)

1, 4, 3, 2, 5. Sync first (1, 4). Then microtasks: the promise logs 3 and schedules timer 5. Then macrotasks in order: timer 2 (scheduled first), then timer 5.

console.log(1);
setTimeout(() => console.log(2));
Promise.resolve().then(() => {
  console.log(3);
  setTimeout(() => console.log(5));
});
console.log(4);

2. What does forEach with async do here?

It logs “done” first, then the items. forEach does not wait for async callbacks. Use for...of with await for sequential work or Promise.all for parallel work.

async function run() {
  [1, 2, 3].forEach(async n => {
    await sleep(100);
    console.log(n);
  });
  console.log('done');
}

// correct, in order:
for (const n of [1, 2, 3]) { await sleep(100); console.log(n); }

3. What is logged? (closure capturing a variable that changes)

2. The closure captures the variable, not its value at creation; by the time show() runs, count is 2.

let count = 1;
const show = () => console.log(count);
count = 2;
show();

4. What is logged? (method extracted from an object)

undefined (in strict mode / modules, an error if you read a property of this). Passing user.greet as a callback loses its this. Bind it or wrap it in an arrow function.

const user = {
  name: 'Asha',
  greet() { console.log(this?.name); },
};
setTimeout(user.greet, 0);              // undefined
setTimeout(() => user.greet(), 0);      // 'Asha'

5. What is logged? (prototype shadowing)

"own", "proto". Property lookup finds an own property first; deleting it reveals the prototype's value.

const proto = { label: 'proto' };
const obj = Object.create(proto);
obj.label = 'own';
console.log(obj.label);
delete obj.label;
console.log(obj.label);

6. What is logged? (async function return value)

Promise { 42 } (a pending or fulfilled promise), not 42. An async function always returns a promise; await it or use .then.

async function answer() { return 42; }
console.log(answer());
answer().then(v => console.log(v));   // 42

7. What is logged? (try/catch around a promise without await)

The catch block does not run, and the rejection becomes unhandled. Without await, the promise rejects after the try block has finished. Add await (or .catch).

async function load() {
  try {
    fetchUser();               // returns a rejected promise — not awaited
  } catch (e) {
    console.log('caught');     // never runs
  }
}

8. What is logged? (getter vs computed once)

1, 2 for the getter, 1, 1 for the snapshot. A getter recomputes on every access; a plain property holds the value computed when the object was created.

let n = 1;
const live = { get value() { return n; } };
const snapshot = { value: n };
n = 2;
console.log(live.value, snapshot.value);   // 2 1

Remember

Key points

  • forEach ignores await

    Use for...of or Promise.all.

  • Async functions return promises

    Always.

  • Callbacks lose this

    Bind or wrap in an arrow.

  • try/catch needs await

    To catch async rejections.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What does Promise.race([]) return? Advanced

    A promise that stays pending forever, because no promise ever settles.

  2. 2 What is logged: console.log([] instanceof Object)? Basic

    true — arrays inherit from Object.prototype through Array.prototype.

  3. 3 Does await pause the whole program? Basic

    No — it pauses only that async function; other code and events keep running.

  4. 4 What is the output of (function () { return typeof arguments; })()? Intermediate

    "object" — arguments is an array-like object (not available in arrow functions).

  5. 5 What happens when you call new on an arrow function? Intermediate

    TypeError — arrow functions are not constructors (no prototype, no own this).

  6. 6 Why does JSON.stringify drop some properties? Intermediate

    Functions, undefined and symbols are omitted; Dates become strings; Map and Set become empty objects.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links