Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 221
lars1595 Avatar asked Nov 12 '16 10:11

lars1595


People also ask

What is promise resolve?

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.

How do you fix undefined promises?

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.

Is not a function promise?

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.


2 Answers

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.

like image 105
Benjamin Gruenbaum Avatar answered Oct 19 '22 18:10

Benjamin Gruenbaum


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.

like image 28
bpinhosilva Avatar answered Oct 19 '22 19:10

bpinhosilva