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?
resolve() The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise.
Fixing “Promise resolver undefined is not a function” js or JavaScript, you may create promise instances yourself using new Promise() . That will fix the problem. The argument is a function providing two arguments: a resolve and a reject function to handle successful and failing promises.
The "then is not a function" error occurs when the then() method is called on a value that is not a promise. To solve the error, convert the value to a promise before calling the method or make sure to only call the then() method on valid promises.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With