I'm using the latest node version 7.1.0 on OSX but I still can't use Promises. I get
index.js
new Promise();
Error:
new Promise(); ^
TypeError: Promise resolver undefined is not a function
Doesn't node 7.1.0 support ES6 and Promise?
I'm using the latest node version 7.1.0 on OSX but I still can't use Promises. I get
index.js
new Promise();
Error:
new Promise(); ^
TypeError: Promise resolver undefined is not a function
Doesn't node 7.1.0 support ES6 and Promise?
The API for promises requires you to pass a function to the promise constructor. Quoting MDN:
new Promise( /* executor */ function(resolve, reject) { ... } );
executor - A function that is passed with the arguments resolve and reject. The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object). The resolve and reject functions, when called, resolve or reject the promise respectively. The executor normally initiates some asynchronous work and then, once that completes, calls either the resolve or reject function to resolve the promise or else reject it if an error occurred.
You can see this answer for usage examples.
Node 7.1 supports promises.
You must provide the callbacks to Promise constructor so it'll know what to do when resolving or rejecting the operation.
For example:
var p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 5000);
});
p.then(() => {
console.log("Got it");
})
After 5 seconds you'll see the message Got it
in your console.
There is a very good library for Promises: Bluebird
Check the MDN documentation as well.
I like this article from Google developers.
You cannot create a new Promise this way without providing some argument. You can however create a promise that resolves to undefined
simply by using Promise.resolve()
.