Your promise implementation is incorrect.
This is how the promise works:
If the handler function:
returns a value, the promise returned by then gets resolved with the returned value as its value.
doesn't return anything, the promise returned by then gets resolved with an undefined value.
throws an error, the promise returned by then gets rejected with the thrown error as its value.
returns an already fulfilled promise, the promise returned by then gets fulfilled with that promise's value as its value.
returns an already rejected promise, the promise returned by then gets rejected with that promise's value as its value.
returns another pending promise object, the resolution/rejection of the promise returned by then will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the resolved value of the promise returned by then will be the same as the resolved value of the promise returned by the handler.
I believe it's better to provide a correct solution or no solution at all, otherwise, it may mislead our fellow developers into believing it to be the correct solution. You may check out this link for a detailed implementation and explanation of promises. By the way great post. :)
Your promise implementation is incorrect.
This is how the promise works:
If the handler function:
The implementation is a rough idea.
I believe it's better to provide a correct solution or no solution at all, otherwise, it may mislead our fellow developers into believing it to be the correct solution. You may check out this link for a detailed implementation and explanation of promises. By the way great post. :)
Please read the note before the answer started.
Yes, I've read that, it says "not production ready", not that the provided solutions are psudo-code. Anyway great stuff 👍.