Master the JavaScript Interview: What is a Promise?

Eric Elliott
Jan 23, 2017 · 11 min read
Image for post
Image for post
Photo by Kabun (CC BY NC SA 2.0)

What is a Promise?

An Incomplete History of Promises

How Promises Work

wait — promise example on CodePen

Important Promise Rules

onFulfilled?: Function,
onRejected?: Function
) => Promise

Promise Chaining

Promise chaining behavior example on CodePen

Error Handling

Image for post
Image for post
Without .catch(), an error in the success handler is uncaught.
Image for post
Image for post
With .catch(), both error sources are handled. (diagram source)

I recommend ending all promise chains with a .catch().

How Do I Cancel a Promise?

Adding .cancel() to the promise

Forgetting to clean up

Forgetting to handle a rejected cancel promise

Overly complex

Rethinking Promise Cancellation

Cancellable wait — try it on CodePen

Abstracting Promise Cancellation

speculation(fn: SpecFunction, shouldCancel: Promise) => Promise
SpecFunction(resolve: Function, reject: Function, onCancel: Function) => Void
npm install --save speculation

Extras of the Native JS Promise


Explore the Series

Image for post
Image for post
Start your free lesson on

JavaScript Scene

JavaScript, software leadership, software development, and…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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