JS 9 Promise Interview Questions

JS 9 Promise Interview Questions

1. Multiple .catch

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

What will be the output of the above code? Please select the correct answer:

  • [ ] Print a message
  • [x] Print the message twice
  • [ ]UnhandledPromiseRejectionWarning
  • [ ] Program exit

Analysis:

We create a Promise using the constructor method and immediately trigger an error via the reject callback.

Then .catch works similar to DOM's .addEventListener(event, callback) or Event Emitter's .on(event, callback) where multiple callbacks can be added. Each is called with the same arguments.

2. Multiple .catch

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))

What will be the output of the above code? Please select the correct answer:

  • [ ] Print a message
  • [ ] Print the message twice
  • [x]UnhandledPromiseRejectionWarning
  • [ ] Program exit

Analysis:

When using the Promise constructor, you must call either the resolve() or reject() callback. The Promise constructor does not use your return value, so it will not actually receive another Promise created by Promise.reject().

When there is no .catch after Promise.reject(), the answer is UnhandledPromiseRejectionWarning.

3. Chaining .then and .catch

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

What will be the output of the above code? Please select the correct answer:

  • [x] Printing error and undefined
  • [ ] Print twice error
  • [ ]UnhandledPromiseRejectionWarning
  • [ ]undefined

Analysis

When chaining .then and .catch, it's helpful to think of them as a series of steps. Each .then receives the value returned by the previous .then as its argument. However, if your "step" encounters an error, any subsequent .then "steps" will be skipped until a .catch is encountered. If you want to override an error, all you have to do is return a non-error value. Can be accessed via any subsequent .then.

Tip: console.log() always returns undefined.

4. Link.catch

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

What will be the output of the above code? Please select the correct answer:

  • [x] Print an error message once
  • [ ] Print the error message twice
  • [ ] UnhandledPromiseRejectionWarning
  • [ ] Program exit

Analysis

When chaining .catch , each one only handles errors raised in the previous .then or `.catch "step". In this example, the first .catch returns console.log, which can only be accessed by adding a .then() after the two .catch.

5. Multiple .catch

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

What will be the output of the above code? Please select the correct answer:

  • [ ] Print a message
  • [ ] Print the message twice
  • [ ] UnhandledPromiseRejectionWarning
  • [x] Do not print anything

Analysis

Tip: .catch can simply ignore (or override) errors by returning a regular value.

This trick only works if the subsequent .then receives the value.

6..then process

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

What will be the output of the above code? Please select the correct answer:

  • [ ] Print "Success!" and "SUCCESS!"
  • [ ] Print "Success!"
  • [x] Print "SUCCESS!"
  • [ ] Do not print anything

Analysis

Tip: .then passes data sequentially, from return value to the next .then(value => /* handle value */).

In order to pass the value to the next .then, return is the key.

7..then process

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

What will be the output of the above code? Please select the correct answer:

  • [ ] Print "SUCCESS!"
  • [ ] Print "Success!"
  • [x] Print "SUCCESS!" and "SUCCESS!"
  • [ ] Do not print anything

Analysis:

There are two console.log calls that will be called.

8..then process

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

What will be the output of the above code? Please select the correct answer:

  • [ ] Print "SUCCESS!"
  • [ ] Print "Success!"
  • [ ] Prints "SUCCESS!" and "SUCCESS!"
  • [x] print undefined

Analysis:

Tip: .then passes data sequentially, from the return value to the next .then(value => /* handle value */).

In order to pass the value to the next .then, return is the key.

9. Flow between .then and .catch

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))

The above are the details of 9 JS Promise interview questions. For more information about JS Promise interview questions, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • A Deep Dive into JavaScript Promises
  • Detailed explanation of Promises in JavaScript
  • Front-end JavaScript Promise
  • How to add abort function to promise in JS
  • Thoroughly understand JavaScript's Promise

<<:  Docker and portainer configuration methods under Linux

>>:  Two solutions to the problem of MySQL in conditional statement only reading one piece of information

Recommend

Example of creating a virtual host based on Apache port

apache: create virtual host based on port Take cr...

How to implement the singleton pattern in Javascript

Table of contents Overview Code Implementation Si...

CSS horizontal centering and limiting the maximum width

A CSS layout and style question: how to balance h...

Handwritten Vue2.0 data hijacking example

Table of contents 1: Build webpack 2. Data hijack...

JS implements the snake game

Table of contents 1. Initialization structure 2. ...

Will CSS3 really replace SCSS?

When it comes to styling our web pages, we have t...

JavaScript css3 to implement simple video barrage function

This article attempts to write a demo to simulate...

Detailed steps to install MySql 5.7.21 in Linux

Preface The most widely used database in Linux is...

The correct way to migrate MySQL data to Oracle

There is a table student in the mysql database, i...

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

How to deal with garbled characters in Mysql database

In MySQL, database garbled characters can general...

Detailed explanation of MySQL database binlog cleanup command

Overview Today I will mainly share how to correct...

A brief discussion on the implementation principle of Vue slot

Table of contents 1. Sample code 2. See the essen...

Detailed explanation of the difference between JavaScript onclick and click

Table of contents Why is addEventListener needed?...