1. Multiple .catchvar 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:
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 .catchvar 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:
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 .catchvar 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:
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.catchvar 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:
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 .catchnew 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:
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 processPromise.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:
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 processPromise.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:
Analysis: There are two console.log calls that will be called. 8..then processPromise.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:
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 .catchPromise.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:
|
<<: Docker and portainer configuration methods under Linux
apache: create virtual host based on port Take cr...
Table of contents Overview Code Implementation Si...
A CSS layout and style question: how to balance h...
Table of contents 1: Build webpack 2. Data hijack...
Table of contents 1. Initialization structure 2. ...
When it comes to styling our web pages, we have t...
This article attempts to write a demo to simulate...
Preface The most widely used database in Linux is...
There is a table student in the mysql database, i...
Table of contents Implementing a search engine ba...
In MySQL, database garbled characters can general...
Overview Today I will mainly share how to correct...
Table of contents 1. Sample code 2. See the essen...
Table of contents Why is addEventListener needed?...
It has been a long time since the last update of ...