Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native Support for Promises in Node.js

Is there native support for promises in current versions of Node.js?

Node.js uses the V8 engine. This JavaScript engine is also used by Chrome, and Chrome 32 has native support for promises. But I can't seem to get promises to work (natively) in Node.js.

I've tried the following code in Chrome 32 and it works.

var promise = new Promise(function(resolve, reject) {   // do a thing, possibly async, then…    if ( 1===1 /* everything turned out fine */) {     resolve("Stuff worked!");   }   else {     reject(Error("It broke"));   } });  promise.then(function( message ) {   console.log( message ); }, function( err ) {   console.log( err ); }); 

However, when I try this same code in Node.js, I get:

var promise = new Promise(function(resolve, reject) {                    ^ ReferenceError: Promise is not defined 

This code is from the excellent tutorial:

http://www.html5rocks.com/en/tutorials/es6/promises/

like image 537
Carl Parker Avatar asked Feb 04 '14 22:02

Carl Parker


People also ask

Are promises native JavaScript?

One of the interesting evolutions of mainstream JavaScript development has been the widespread adoption of Promises. Promises simplify asynchronous code. Since JavaScript in the browser uses a single-threaded, callback-based programming model, asynchronicity is everywhere.

How do I use promises in Node JS?

A custom promise can be created by using a node module called 'q. ' The 'q' library needs to be downloaded and installed using the node package manager. After using the 'q' library, the method “denodeify” can be called which will cause any function to become a function which returns a promise.

Does Nodejs have promises?

A Node. js Promise is a placeholder for a value that will be available in the future, allowing us to handle the result of an asynchronous task once it has completed or encountered an error. Promises make writing asynchronous code easier. They're an improvement on the callback pattern and very popular in Node.

Which are promises states in Node JS?

A Promise is in one of these states: pending: initial state, neither fulfilled nor rejected. fulfilled: meaning that the operation was completed successfully. rejected: meaning that the operation failed.


1 Answers

Although Node.js added native promise in stable version 0.12.

But due to the memory leak issue, I recommend to use bluebird to avoid the issue.

  • https://github.com/promises-aplus/promises-spec/issues/179
  • https://github.com/tj/co/issues/180

Old anwser:

Node.js added native promise support since version 0.11.13.

nvm install 0.11.12 nvm run 0.11.12 > Promise ReferenceError: Promise is not defined > console.log(process.versions.v8) 3.22.24.19  nvm install 0.11.13 nvm run 0.11.13 > Promise [Function: Promise] > console.log(process.versions.v8) 3.25.30 

Note: Node.js v0.11 is still in beta, be careful if use it in production.

like image 198
Chris Avatar answered Sep 29 '22 03:09

Chris