Friday, February 2, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...