Node 7.1.0 new Promise () resolver undefined is not a function

I am using the latest version of node version 7.1.0 on OSX, but I still cannot use Promises. I get

index.js

new Promise(); 

Error:

 new Promise(); ^ 

TypeError: Promise resolver undefined is not a function

Doesn't support node 7.1.0 ES6 and Promise?

+6
source share
2 answers

The promises API requires you to pass a function to the promise constructor. Quoting MDN :

new Promise (/ * executor * / function (enable, reject) {...});

performer . A function that is passed with arguments allows and rejects. The executor function is immediately executed by the Promise implementation, passing the permission and reject functions (the executor is called before the Promise constructor even returns the created object). The allow and reject functions, when invoked, allow or reject the promise, respectively. The contractor usually initiates some asynchronous work, and then, upon completion, calls the enable or reject function to resolve the promise or rejects it if an error occurs.

You can see this answer for usage examples.

Node 7.1 supports promises.

+11
source

You must provide callbacks to the Promise constructor so that it knows what to do when you allow or decline the operation.

For instance:

 var p = new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, 5000); }); p.then(() => { console.log("Got it"); }) 

After 5 seconds, the message Got it appears in the console.

There is a very good library for Promises: Bluebird

Check out the Google developer documentation.

+5
source

Source: https://habr.com/ru/post/1012260/


All Articles