By Dan Levy
Translation: Crazy geek
Original text: danlevy.net/javascript-…
Reproduced without permission
👇 Please complete the following 9 questions 👇
More than 1..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))
Copy the code
What will be the output of the above code? Please choose the correct answer:
-
Print a message once
-
Print messages twice
-
UnhandledPromiseRejectionWarning
-
Program exits
Resolution:
We use the constructor method to create a Promise and immediately fire an error with the Reject callback.
Then.catch works like DOM’s.addeventListener (event, callback) or Event Emitter’s.on(event, callback), where ** can add multiple callbacks. ** Each is called with the same arguments.
More than 2..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))
Copy the code
What will be the output of the above code? Please choose the correct answer:
-
Print a message once
-
Print messages twice
-
UnhandledPromiseRejectionWarning
-
Program exits
Resolution:
When the Promise constructor is used, the resolve() or Reject () callback must be called. The Promise constructor doesn’t use your return value, so it doesn’t actually receive any more promises created by promise.reject ().
In Promise. Reject () no. After the catch, the answer is UnhandledPromiseRejectionWarning.
Links to 3..then
和 .catch
var p = new Promise((resolve, reject) = > {
reject(Error('The Fails! '))
})
.catch(error= > console.log(error))
.then(error= > console.log(error))
Copy the code
What will be the output of the above code? Please choose the correct answer:
-
Print errors and undefined
-
Print two errors
-
UnhandledPromiseRejectionWarning
-
undefined
parsing
When linking.then and.catch, it helps to think of them as a series of steps. Each.then accepts as its argument the value returned by the previous.then. However, if your “step” encounters an error, any subsequent. Then steps will be skipped until a. Catch is encountered. To override an error, all you have to do is return a non-error value. It can be accessed through any subsequent.then.
Tip: console.log() always returns undefined.
Links to 4..catch
var p = new Promise((resolve, reject) = > {
reject(Error('The Fails! '))
})
.catch(error= > console.log(error.message))
.catch(error= > console.log(error.message))
Copy the code
What will be the output of the above code? Please choose the correct answer:
-
Print an error message
-
Prints two error messages
-
UnhandledPromiseRejectionWarning
-
Program exits
parsing
When linking.catch, each handles only errors raised in previous.then or.catch “steps”. In this case, the first.catch returns console.log, which can only be accessed by adding.then() ‘after two.catches.
More than 5..catch
new Promise((resolve, reject) = > {
resolve('Success! ')
})
.then((a)= > {
throw Error('Oh noes! ')
})
.catch(error= > {
return "actually, that worked"
})
.catch(error= > console.log(error.message))
Copy the code
What will be the output of the above code? Please choose the correct answer:
-
Print a message once
-
Print messages twice
-
UnhandledPromiseRejectionWarning
-
Nothing is printed
parsing
Tip:.catch can simply ignore (or override) an error by returning a regular value.
This trick only works if the value is received by a subsequent.then.
6. .then
Flow between
Promise.resolve('Success! ')
.then(data= > {
return data.toUpperCase()
})
.then(data= > {
console.log(data)
})
Copy the code
What will be the output of the above code? Please choose the correct answer:
- Print “Success!” And “SUCCESS!”
- Print “Success!”
- Print “SUCCESS!”
- Nothing is printed
parsing
Then (value => /* Handle value */) then(value => /* Handle value */)
In order to pass the value to the next.then, return is key.
7. .then
Flow between
Promise.resolve('Success! ')
.then(data= > {
return data.toUpperCase()
})
.then(data= > {
console.log(data)
return data
})
.then(console.log)
Copy the code
What will be the output of the above code? Please choose the correct answer:
- Print “SUCCESS!”
- Print “Success!”
- Print “SUCCESS!” And “SUCCESS!”
- Nothing is printed
Resolution:
Two console.log calls will be made.
8. .then
Flow between
Promise.resolve('Success! ')
.then(data => {
data.toUpperCase()
})
.then(data => {
console.log(data)
})
Copy the code
What will be the output of the above code? Please choose the correct answer:
- Print “SUCCESS!”
- Print “Success!”
- Print “SUCCESS!” And “SUCCESS!”
- print
undefined
Resolution:
Then (value => /* Handle value */). Then (value => /* Handle value */)
In order to pass the value to the next.then, return is key.
9. .then
和 .catch
Flow between
Promise.resolve('Success! ')
.then((a)= > {
throw Error('Oh noes! ')
})
.catch(error= > {
return 'actually, that worked'
})
.then(data= > {
throw Error('The fails! ')
})
.catch(error= > console.log(error.message))
Copy the code
What will be the output of the above code? Please choose the correct answer:
-
Print “Oh noes!” And “The fails!”
-
Print “Oh noes!” “
-
Print “The fails!”
-
Print “Actually, that worked”
-
Nothing is printed
Resolution: