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

JS+AJAX realizes the linkage of province, city and district drop-down lists

This article shares the specific code of JS+AJAX ...

How to create a Pod in Kubernetes

Table of contents How to create a Pod? kubectl to...

Example of deploying Laravel application with Docker

The PHP base image used in this article is: php:7...

Ubuntu16.04 builds php5.6 web server environment

Ubuntu 16.04 installs the PHP7.0 environment by d...

Optimization methods when Mysql occupies too high CPU (must read)

When Mysql occupies too much CPU, where should we...

HTML table markup tutorial (5): light border color attribute BORDERCOLORLIGHT

In a table, you can define the color of the upper...

Detailed steps for setting up a nexus server

1. The significance of building nexus service As ...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

Detailed explanation of Nginx passively checking the server's survival status

introduce Monitors the health of HTTP servers in ...

Comparing the performance of int, char, and varchar in MySQL

There are many seemingly true "rumors" ...

Detailed explanation of Nginx's control over access volume

Purpose Understand the Nginx ngx_http_limit_conn_...

MySQL uses SQL statements to modify table names

In MySQL, you can use the SQL statement rename ta...