Problem, as an interviewer would state it:
"Implement a MyPromise class with a constructor that takes an executor, then, catch, and static resolve and reject. It must handle chaining and thenables. Discuss edge cases, runtime, and alternatives."
Examples:
MyPromise.resolve(1).then((v) => v + 1).then((v) => { throw new Error("boom " + v); })
.catch((e) => e.message).then((v) => v + "!"); // "boom 2!"
MyPromise.resolve({ then(resolve) { resolve("adopted"); } }); // a thenable is adopted: "adopted"
Clarifying questions expected:
- Which specification should it follow (Promises/A+), and how asynchronous must the callbacks be?
- Should it accept any thenable (an object with a
thenmethod), not just its own instances? - Which extras are wanted:
finally,all,race, unhandled-rejection reporting?
Code / implementation expected: Yes — a real, working class, ideally run against the official Promises/A+ conformance suite.