27

JavaScript Promises: 9 Questions - Dan Levy's Programming Blog

 4 years ago
source link: https://danlevy.net/javascript-promises-quiz/?s=r
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JavaScript Promises: 9 Questions

Learn about promises & take the quiz!

updated 12 months ago
published about 1 year ago on Nov 26th, 2019

Do you know your JavaScript & Promises???

  • Prove your JavaScript skillz! 🚀
  • No login or signup required. ✨
  • Multiple choice. 🤖 … How difficult can it be, eh?

My goal is to try a new ‘quiz tool’ I built for my blog, and get feedback on how well you learned from the excercise.

If you get stuck

  1. Read the Hints (Green button, lower left corner). Some include a few animated answers w/ detailed annotations, while others offer only a few clues. I can’t give away all the answers so easy!
    Seriously though, this is intentional, the answers are there. Skip ahead or try the code snippets out, then change it, and play some more.
  2. Try the code in your browser’s Console (try shortcut F12 or search it) or use repl.it*.
  3. Please feel free to Tweet at me @justsml. I’d love to hear your thoughts!

👇 Complete 9 Questions Below👇

1. Multiple .catch’s

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

Please select the closest answer:
  • print message once
  • print message twice
  • UnhandledPromiseRejectionWarning
  • process exits

2. Multiple .catch’s

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

Please select the closest answer:
  • print message once
  • print message twice
  • UnhandledPromiseRejectionWarning
  • process exits

3. Chaining .then and .catch’s

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

What will the output be?

Please select the closest answer:
  • print error and `undefined`
  • print error twice
  • UnhandledPromiseRejectionWarning
  • undefined

4. Chaining .catch’s

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

What will the output be?

Please select the closest answer:
  • print error message once
  • print error message twice
  • UnhandledPromiseRejectionWarning
  • process exits

5. Multiple .catch’s

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

Please select the closest answer:
  • print message once
  • print message twice
  • UnhandledPromiseRejectionWarning
  • nothing prints

6. Flow between .then’s

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

What will the output be?

Please select the closest answer:
  • print "Success!" and "SUCCESS!"
  • print "Success!"
  • print "SUCCESS!"
  • nothing prints

7. Flow between .then’s

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

What will the output be?

Please select the closest answer:
  • print "SUCCESS!"
  • print "Success!"
  • print "SUCCESS!" and "SUCCESS!"
  • nothing prints

8. Flow between .then’s

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

What will the output be?

Please select the closest answer:
  • print "SUCCESS!"
  • print "Success!"
  • print "SUCCESS!" and "SUCCESS!"
  • prints `undefined`

9. Flow between .then’s and .catch’s

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

What will the output be?

Please select the closest answer:
  • print "Oh noes!" and "The fails!"
  • print "Oh noes!"
  • print "The fails!"
  • print "actually, that worked"
  • nothing prints

Your score is 0 / 9

SHARE
Dan Levy

Dan Levy is an accomplished programmer, teacher, speaker, cat dad and writer.

Some of Dan’s documentation work is seen by countless developers visiting the Mozilla Developer Network site to learn about the Fetch & Promise APIs.

His contributions span dozens of well-known projects, including NodeJS, Docker, TypeScript, React Router, GatsbyJS, Bluebird, Lodash, Rancher, Minio, Slate, IxJS, and Functional Promises.

Dan is available for consulting and contract work!
With a special focus on Remote Work consulting, System Scaling, Performance Audits & Recommendations, Architectural Guidance, and Cloud Cost Reduction Interventions!

Feel free to grab a spot on Dan’s calendar, or reach out via Twitter DMs or the contact form.

Unless otherwise noted, all content is copyright Dan Levy 2014-1019.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK