Dan Levy's Avatar DanLevy.net

JS Quiz: 9 Promise Questions

Never drop a promise again!

JS Quiz: 9 Promise Questions

Do you know JavaScript Promises?

  1. Check for Hints (Big button, bottom corner).
  2. Try the code in your browser’s Console (try shortcut F12 or search it) or use repl.it*.
  3. Please feel free to Tweet at me @justsml. I’d love to hear your thoughts!

👇 Complete 9 Questions Below👇

1. 

Handling Errors

What will the output be for the following code?

var p = new Promise((resolve, reject) => {
reject(Error('The Fails!'))
})
p.catch(error => console.log(error.message))
p.catch(error => console.log(error.message))

We create a Promise using the constructor method, triggering an error immediately with the reject callback.

Then the .catch handlers work like the DOM’s .addEventListener(event, callback) or Event Emitter’s .on(event, callback) where multiple handler callbacks can be added. Each will be called with the same arguments.

2. 

Handling Errors

What will the output be for the following code?

var p = new Promise((resolve, reject) => {
return Promise.reject(Error('The Fails!'))
})
p.catch(error => console.log(error.message))
p.catch(error => console.log(error.message))

When using the Promise constructor you must invoke either resolve() or reject() callbacks. The Promise constructor doesn’t use your return value, so the additional Promise created with Promise.reject() will effectively never be heard from again.

5. 

Handling Errors

What will the output be for the following code?

new Promise((resolve, reject) => {
resolve('Success!')
})
.then(() => {
throw Error('Oh noes!')
})
.catch(error => {
return "actually, that worked"
})
.catch(error => console.log(error.message))

Hint: .catch’s can be used to ignore (or override) errors simply by returning a regular value.

This trick works only when there is a subsequent .then to receive the value.

6. 

Handling Data

What will the output be for the following code?

Promise.resolve('Success!')
.then(data => {
return data.toUpperCase()
})
.then(data => {
console.log(data)
})

Hint: .then’s pass data sequentially, from return value to the next .then(value => /* handle value */).

A return is key in order to pass a value to the next .then.

7. 

Handling Data

What will the output be for the following code?

Promise.resolve('Success!')
.then(data => {
return data.toUpperCase()
})
.then(data => {
console.log(data)
return data
})
.then(console.log)

There are 2 console.log calls which will be called.

8. 

Handling Data

What will the output be for the following code?

Promise.resolve('Success!')
.then(data => {
data.toUpperCase()
})
.then(data => {
console.log(data)
})

Hint: .then’s pass data sequentially, from return value to the next .then(value => /* handle value */).

A return is key in order to pass a value to the next .then.

9. 

Handling Data

What will the output be for the following code?

Promise.resolve('Success!')
.then(() => {
throw Error('Oh noes!')
})
.catch(error => {
return 'actually, that worked'
})
.then(data => {
throw Error('The fails!')
})
.catch(error => console.log(error.message))

annotated-code/question-9-4.webp

Quiz Score:

Congrats! Quiz completed.

Edit on GitHubGitHub