Friday, 2 February 2018

javascript - Why is it possible to pass in a non-function parameter to Promise.then() without causing an error?

I have the following:



new Promise(resolve => setTimeout(resolve, 2000))
.then(() => console.log("after 2 seconds"));

new Promise(resolve => setTimeout(resolve, 3000))

.then(console.log("before 3 seconds (instantly)"));


which produces the following output:



> node index.js
before 3 seconds (instantly)
after 2 seconds



Promise.then() expects a onFulfilled function, but I passed in console.log("before 2 seconds (instantly)"), which is not a function. Two-part question:




  • Why does console.log("before 2 seconds (instantly)") get executed right away (or at all)?

  • Why didn't the second Promise raise an exception when I didn't pass in a function?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...