JavaScript Course
JavaScript
/
Advanced

Promise Polyfill / Mock

Definition

One of the hardest interview questions: Implement the core functionality of a Promise from scratch. It requires deep understanding of the Event Loop, Callbacks, and State Machines.

Explain Like I'm New

The interviewer wants to see if you understand that a Promise is just an object with a state (pending, fulfilled, rejected), a value, and an array of callbacks (`.then`) that are triggered asynchronously when the state changes.

Real World Example

Writing a custom `MyPromise` class with `resolve`, `reject`, and `then` methods.

Common Use Cases

  • •Senior Staff Engineering Interviews

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What are the three states of a Promise?

intermediate

  • Why do you need an array to store `.then` callbacks instead of just a single variable?

advanced

  • Write a simplified implementation of a Promise.

Flash Cards

Question

What are the three states?

Click to reveal answer
Answer

Pending, Fulfilled, and Rejected. Once a promise transitions out of Pending, its state and value are permanently locked.

Question

Why an array for .then callbacks?

Click to reveal answer
Answer

Because you can chain multiple `.then()` calls to the same promise (e.g., `const p = fetch(); p.then(cb1); p.then(cb2);`). When the promise resolves, it must iterate through the array and fire all attached callbacks.