Handling asynchronous scenarios in a Promise implementation can be mind-blowing. Not so much if you embrace the functional programming paradigm. Though guess I fell short of properly explaining how it works exactly in Nancy, evident by the questions I received via direct messages and comments on social media. I try to explain a bit more here, assuming you commanded the Nancy article.

To refresh our memory, here is the final code:

In acync scenarios, resolve or reject are not called immediately. In our example:

const delay = milliseconds => new Nancy(resolve => setTimeout(resolve, milliseconds));

resolve will be called after milliseconds by setTimeout. …

Image for post
Image for post

Ever wondered how JavaScript Promise works internally? Obviously there is some native browser support involved, right? Nope! We can implement the Promise interface using pure JavaScript, examples are libraries like Bluebird or Q. And it’s much simpler than you may think, we can do so in only 70 lines of code! This will help with gaining a deeper insight into Promises by demystifying the underlying formation. Can also serve as a good interview question, if you are an evil employer (don’t be!). Let’s dig into it!

First thing that you notice is that a Promise has three states, so should…


Keyvan M. Sadeghi

Keyvan is co-founder and CTO of Assister.Ai, currently on a mission to bring natural language to web apps. Give him Skynet already!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store