Advertisement
skaterksa

js promises vs callbacks.

Aug 30th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Rewrite the following using promises instead of callbacks.
  2. const f = (firstName, callback) => {
  3.   setTimeout(() => {
  4.     if (!firstName) return callback(new Error('firstName is required'))
  5.     const fullName = `${firstName} Smith`
  6.     return callback(fullName)
  7.   }, 2000)
  8. }
  9.  
  10. f('Andrew', console.log)
  11. f(null, console.log)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement