Question 1

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 is the output?

  • A. Print ‘The Fails! ‘
  • B. Print twice ‘The Fails! ‘
  • C. UnhandledPromiseRejectionWarning
  • D. The process exits

The example uses the constructor to create a Promise, then uses the Reject callback to immediately trigger an error. The.catch handler is similar to the DOM’s.addeventListener (event, callback) or event Emitter. On (event, callback). Multiple callback functions can be added. Therefore, each callback function is called and receives the same parameters.

Answer: B

Second question

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 is the output?

  • A. Print ‘The Fails! ‘
  • B. Print twice ‘The Fails! ‘
  • C. UnhandledPromiseRejectionWarning
  • D. The process exits

If you use a constructor to create a Promise, you must call resolve or reject. Here in the constructor returns a new Promise, outer layer of the Promise and don’t use the return value, the internal no Promise behind. The catch, so the answer is UnhandledPromiseRejectionWarning

Answer: C

The third question

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

What is the output?

  • A. Typographical errors andundefined
  • B. Print two errors
  • C. UnhandledPromiseRejectionWarning
  • D. undefined

When you chain calls.then and.catch, you can think of it as a series of steps. Each.then receives the return value of the previous.then as an argument. However, if an error occurs in one step, subsequent.then will be skipped until a.catch is reached. If you want to override an error, simply return a non-error value. So that it can be retrieved by subsequent dot then. Tip: console.log() always returns undefined.

Answer: A,

Number 4

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 is the output?

  • A. Print an error message
  • B. Error information is displayed twice
  • C. UnhandledPromiseRejectionWarning
  • D. The process exits

When a.catch is called chained, each.catch handles only errors thrown by the preceding.then or.catch. In this case, the first.catch return value is console.log, which can only be obtained by adding a.then to two.catches.

Answer: A,

5

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

What is the output?

  • A. print message once
  • B. print message twice
  • C. UnhandledPromiseRejectionWarning
  • D. nothing prints

.catch can ignore errors by returning normal values. So if you put a dot then here you can get the value back.

Answer: D

Number 6

Promise.resolve('Success! ')
  .then(data => {
    return data.toUpperCase()
  })
  .then(data => {
    console.log(data)
  })
Copy the code
  • A. print “Success!” And “SUCCESS!”
  • B. print “Success!”
  • C. print “SUCCESS!”
  • D. not print at all

The value returned by. Then is passed one by one to subsequent. Then.

Answer: C

Question 7

Promise.resolve('Success! ')
  .then(data => {
    return data.toUpperCase()
  })
  .then(data => {
    console.log(data)
    return data
  })
  .then(console.log)
Copy the code
  • A. print “SUCCESS!”
  • B. print “Success!”
  • C. print “SUCCESS!” and “SUCCESS!”
  • D. nothing prints

As in the previous problem, console.log is called twice.

Answer: C

8 the topic

Promise.resolve('Success! ')
  .then(data => {
    data.toUpperCase()
  })
  .then(data => {
    console.log(data)
  })
Copy the code
  • A. print “SUCCESS!”
  • B. print “Success!”
  • C. print “SUCCESS!” and “SUCCESS!”
  • D. prints undefined

So the first dot then doesn’t return, so the next dot then doesn’t receive a value.

Answer: D

Question 9

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))
Copy the code
  • A. print “Oh noes!” and “The fails!”
  • B. print “Oh noes!”
  • C. print “The fails!”
  • D. print “actually, that worked”
  • E. nothing prints

Obviously, the first error has already been handled by the first.catch, so the second.catch catches the error before it.

Answer: C

For more technical trends and resources, please follow the wechat official number: 1024 translation station