JavaScrit Promises - Basics Quiz

Quiz 4 11 questions

Are you ready?

11 questions to solve

  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
How do we create a new promise?
A promise is created by calling the Promise() constructor. See more at JavaScript Promises — Basics.
In promise terminology, what do we call the function that's passed to the Promise() constructor as an argument?
The function is referred to as the executor. See more at JavaScript Promises — Basics.
The executor function f is executed asynchronously upon calling new Promise(f). True or false?
Clearly false! The executor function is executed synchronously when a new promise object is instantiated. See more at JavaScript Promises — Basics.
Suppose that you've been told that p is a promise. What will typeof p return?
As discussed at the start of the JavaScript Promises — Basics chapter, a promise is an object in JavaScript. Likewise it returns "object" when inspected using typeof.
According to the ES6 specification, a promise can be in one of the three states... What are they?
The three states are pending, fulfilled and rejected. This goes with choice (B). For more details about how these states work, please refer to JavaScript Promises — Basics.
What will the following code log, after 3 seconds?
var p = new Promise(function(resolve, reject) {
   setTimeout(function() {
   }, 3000);

p.then(function(data) {
   console.log('OK ' + data);
After 3 seconds, resolve("Good") is called from within the timeout, which fires the function queued up by then() in line 7, with an argument "Good". The function logs "OK" + data, where data is equal to "Good", hence we get "OK Good" logged.
Which parameter of the executor serves to 'fulfill' its respective promise?
The first parameter of the executor, known as resolve, fulfills its respective promise. This goes with choice (A).
In the code shown below, what is p's internal slot [[PromiseValue]] equal to, once p gets fulfilled by calling resolve() in line 3?
var p = new Promise(function(resolve, reject) {
   setTimeout(function() {
   }, 3000);
Whichever argument is passed to resolve(), is the value with which the corresponding promise is fulfilled i.e the value that goes into [[PromiseValue]]. In this case we call resolve(), with no arguments, or better to say with an undefined argument. Thus p's [[PromiseValue]] will be equal to undefined.

See more at JavaScript Promises — Basics.
Promises simplify the task of writing complex asynchronous code. True or false?
This is discussed in detail in the Promises Introduction chapter.
What does the following code log?
var p = new Promise(function(resolve, reject) {

p.then(function(data) {

The anonymous function passed to then() (in line 5) is executed asynchronously, which means that 'Second' is logged before 'First'. This goes with choice (B). To understand why this happens please refer to JavaScript Promises — Basics.
If the then() method is invoked on a promise when it's settled, it executes the corresponding callback argument synchronously. True or false?
Absolutely not! If then() is called on a settled promise, it executes the given callback asynchronously.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage