DEV Community

Discussion on: All front end Interview questions asked during my recent job hunt.

Collapse
rohanhacker profile image
रोहन मल्होत्रा

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.
Collapse
devabhijeet profile image
Abhijeet Yadav Author

The implementation is a rough idea.

Collapse
vibhanshu909 profile image
vibhanshu pandey

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. :)

Thread Thread
devabhijeet profile image
Abhijeet Yadav Author

Please read the note before the answer started.

Thread Thread
vibhanshu909 profile image
vibhanshu pandey

Yes, I've read that, it says "not production ready", not that the provided solutions are psudo-code. Anyway great stuff 👍.