DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumJavaScript

Build an Asynchronous Value Container That Chains Handlers and Propagates Errors

2 views

Technical Interview Challenge: The "Micro-Runtime" Promise Engine

Context

You are part of a team building a specialized, lightweight JavaScript runtime for edge computing. To minimize the binary size, you’ve decided to exclude the full V8 engine and implement core language features manually. One of the most critical components is the Asynchronous Orchestration Layer.

You are tasked with building a custom Promise implementation that adheres to the Promises/A+ behavioral standards, ensuring that asynchronous tasks are handled predictably without blocking the main execution thread.


Problem Statement

Implement a class MyPromise that replicates the core functionality of the native JavaScript Promise. Your implementation must manage state transitions and coordinate the execution of dependent callbacks via the Microtask Queue.

Requirements:

  1. State Management: The promise must transition from PENDING to either FULFILLED or REJECTED. Once settled, the state and value must become immutable.

  2. Executor Execution: The constructor must take an executor function that receives resolve and reject arguments.

  3. Asynchronous Chaining:

    • Implement .then(onFulfilled, onRejected), which returns a new MyPromise to allow for chaining.

    • Handlers passed to .then must be executed asynchronously using the microtask queue (e.g., queueMicrotask), even if the promise is already settled.

  4. Error Handling: Implement .catch(onRejected) as a shorthand for error management.

  5. Value Forwarding: If a .then handler returns a value, the next promise in the chain should resolve with that value. If it returns another MyPromise, the chain must wait for that promise to settle before continuing.


Example Use Cases

Example 1: Basic Resolution & Chaining

JavaScript

const p = new MyPromise((resolve) => {
  resolve(10);
});

p.then(val => val * 2)
 .then(val => console.log(val)); 
// Expected Output (Async): 20

Example 2: Asynchronous Delay

JavaScript

new MyPromise((resolve) => {
  setTimeout(() => resolve("Data Loaded"), 100);
})
.then(data => data.toUpperCase())
.then(result => console.log(result));
// Expected Output (After 100ms): "DATA LOADED"

Example 3: Error Propagation

JavaScript

new MyPromise((_, reject) => {
  reject(new Error("Network Failed"));
})
.catch(err => console.error(err.message));
// Expected Output: "Network Failed"

Interview Evaluation Criteria

  • The "Zalgo" Test: Does your implementation guarantee that .then callbacks always run asynchronously, even for immediate resolutions?

  • Chainability: Does every call to .then return a unique promise instance?

  • State Locking: Does your implementation prevent a promise from changing its value once it has been resolved or rejected?

  • Microtask Awareness: Do you correctly distinguish between the Macrotask queue (setTimeout) and the Microtask queue (queueMicrotask)?

Sample Test Cases

Case 1
Input
resolve(42).then(v=>v*2)
Expected Output
84
Case 2
Input
reject("err").catch(e=>e)
Expected Output
"err"
Case 3
Input
async resolve after timeout
Expected Output
resolves correctly

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
2
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript