Introduction
-
What is the problem with using callbacks to handle nested asynchronous tasks?
While handling nested async tasks using just callbacks, there's high chance to run into what's known as the 'callback hell'. It's simply extremely indented code that's exceptionally difficult to visually digest, maintain, and debug. Learn more in JavaScript Promises — The limitations of callbacks.
-
What was the Promises/A specification?
The Promises/A spec was drafted by the CommonJS community to kind-of standardize the interface of a promise object, and some other functionalities surrounding promises. Learn more in JavaScript Promises — The history of promises.
-
What was the Promises/A+ specification?
The Promises/A+ spec was meant to build upon the Promises/A spec, improving it where was underspecified. Promises in the ECMAScript 2015 standard were made to conform to this Promises/A+ spec. Learn more in JavaScript Promises — The history of promises.
-
In which version of ECMAScript were promises introduced?
Promises were introduced in the ECMAScript 2015 version (also known as ECMAScript 6). Learn more in JavaScript Promises — The history of promises.
-
What is a promise?
A promise is an intermediary object meant to placehold the outcome of a given async operation. Learn more in JavaScript Promises — What are promises?
-
Give three benefits of using promises over naive callbacks?
Here are four benefits instead of just three:
- Promises mitigate the extra levels of indentation otherwise present in callback code.
- Error handling in promises is much easier as compared to that in callbacks.
- Promises can be passed around a program, since they're merely objects, with different parts of the program responding to the outcome of the underlying async operation as they want to.
- Using
async/await
, which are made to be used with promises, we can make async code look as if it's synchronous code.
Learn more in JavaScript Promises — Benefits of using promises.
-
Name the interface used to work with promises in JavaScript.
The
Promise
interface is used to work with promises in JavaScript.
Basics
-
How to create a promise in JavaScript?
Using the
Promise()
constructor. Learn more in JavaScript Promises — Creating a promise. -
What must be the type of the argument provided to the
Promise()
constructor?The argument to
Promise()
must be a function. Learn more in JavaScript Promises — Creating a promise. -
What is this argument usually referred to as?
As per the ECMAScript spec, the argument to
Promise()
is referred to as the executor function. Learn more in JavaScript Promises — Creating a promise. -
What is the purpose of this argument?
The executor function is used to execute an asynchronous operation, and then resolve or reject the underlying promise in the future when the async operation completes (either due to success or failure). Learn more in JavaScript Promises — Creating a promise.
-
The executor function is invoked with 2 arguments. What are these arguments conventionally named?
The arguments provided to the executor function are conventionally named
resolve
andreject
, in that very order. Learn more in JavaScript Promises — Creating a promise. -
What is the purpose of these arguments provided to the executor?
The arguments provided to the executor function, namely
resolve
andreject
, are meant to either resolve or reject the underlying promise, respectively, to ultimately execute all the callbacks set up on the promise (for e.g. by virtue of invocations ofthen()
). Learn more in JavaScript Promises — Creating a promise. -
Give the three states of a promise and describe them.
The three states are as follows:
- Pending — the underlying async operation of the promise is still ongoing in the background.
- Fulfilled — the underlying async operation of the promise has successfully completed.
- Rejected — the underlying async operation of the promise has failed.
Learn more in JavaScript Promises — States of a promise.
-
What is meant by an unsettled promise?
An unsettled promise is just another way to refer to a pending promise. Learn more in JavaScript Promises — States of a promise.
-
What is meant by a settled promise?
A settled promise is one that has been either resolved or rejected. Or another way to look at it is that a settled promise is one that is not pending. Learn more in JavaScript Promises — States of a promise.
-
What is meant by the 'value' of a promise?
The value of a promise is the datum with which it either gets resolved or gets rejected (in this case, it's usually an error object). Learn more in JavaScript Promises — The value of a promise.
-
What is the purpose of the
then()
method of thePromise
interface?The
then()
method is used to set up a callback to be executed when the underlying promise gets resolved (via its first argument) or rejected (via its second argument). Learn more in JavaScript Promises — Thethen()
method. -
What are the arguments provided to
then()
conventionally referred to as?The arguments provided to
then()
are conventionally referred to asonFulfilled
andonRejected
, in that very order. Learn more in JavaScript Promises — Thethen()
method. -
The executor function is executed synchronously by the
Promise()
constructor. True or false?True; the
Promise()
constructor invokes the executor function provided to it immediately, synchronously. Learn more in JavaScript Promises — The executor function. -
The
Promise()
constructor can be called without an argument. True or false?False; the
Promise()
constructor requires an argument to be provided to it, and that it must be a function. Learn more in JavaScript Promises — The executor function. -
What happens when
then()
is called on an unsettled promise?The respective callback is queued up in an internal callback queue maintained by the promise. This queue is emptied and each callback in there executed, as a microtask, when the promise is resolved or rejected (by virtue of the promise's executor calling
resolve()
orreject()
, respectively). Learn more in JavaScript Promises — Howthen()
works under the hood? -
What happens when
then()
is called on a settled promise?The respective callback is executed immediately, as a microtask. Learn more in JavaScript Promises — How
then()
works under the hood?