Skip to content
Advertisement

Promise not fulfilled

This is a basic question. I’m working through a js/node workshop on async programming called promise-it-wont-hurt. I have the following exercise:

JavaScript

my test.js file contains:

JavaScript

When I run “node test.js” at the command line , I get no output. What am I doing wrong?

Advertisement

Answer

All this does is return the string 'FULFILLED!':

JavaScript

But it doesn’t return it to anywhere. setTimeout certainly doesn’t do anything with that result, and neither does the Promise. To fulfill the Promise with a value, you have the fulfill function provided by the Promise itself:

JavaScript

(This is more commonly called resolve, but it doesn’t really matter what you call it as long as it’s the first parameter in the function passed to the Promise constructor.)

As you can imagine, to reject the Promise you’d call the reject function similarly.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement