Node.js中对Promise的本机支持

Car*_*ker 75 javascript node.js promise

在当前版本的Node.js中是否支持promises?

Node.js使用V8引擎.Chrome也使用此JavaScript引擎,Chrome 32本身也支持承诺.但我似乎无法在Node.js中获得承诺(本机).

我在Chrome 32中尝试了以下代码,它可以运行.

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 );
});
Run Code Online (Sandbox Code Playgroud)

但是,当我在Node.js中尝试相同的代码时,我得到:

var promise = new Promise(function(resolve, reject) {
                   ^
ReferenceError: Promise is not defined
Run Code Online (Sandbox Code Playgroud)

这段代码来自优秀的教程:

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

Chr*_*ris 56

虽然Node.js在稳定版本中添加了原生承诺0.12.

但由于内存泄漏问题,我建议使用bluebird以避免此问题.


老安瑟:

Node.js自版本以来添加了本机承诺支持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
Run Code Online (Sandbox Code Playgroud)

注意:Node.js v0.11仍处于测试阶段,如果在生产中使用它,请小心.

  • 有没有人知道所提到的内存泄漏是否仍然存在于节点4/5中? (2认同)

you*_*rrr 42

我知道OP发布这篇帖子已经有很长一段时间了,但是我想更新那些仍然通过搜索找到这个问题的人.

Node.js为Promises添加了原生支持,因为它与io.js合并.这件事发生在2015年9月8日(按官方网站的Node.js新闻岗位)与节点V4.0.0的第一个稳定版本.

许多新的ES6功能(例如Promises)随版本4一起添加.您可以在此处阅读有关它们的更多信息.

编辑:值得注意的是,似乎Bluebird的Promise比Promise的本机实现表现得更好.


Nuc*_*eon 39

重要的是要指出,这个问题的接受答案是使用一个promise库,它在语法上与Promises/A +规范定义的本机JS promise功能完全相同.

如果您希望Node尽可能地模仿浏览器,那么您应该使用es6-promise位于https://github.com/jakearchibald/es6-promise的模块.

npm install es6-promise

var Promise = require("es6-promise").Promise

技术差异:

模块中的一个关键区别在于构造函数:var foo = new Promise()在浏览器中无效,因为Promise构造函数需要初始化函数,并且该函数将负责解析或拒绝该承诺.在node-promises模块中,Promise的所有用户都可以访问resolve,这打破了Promise的设置.

使用示例 node-promises

var foo = new Promise() 
// no encapsulation, now anyone you pass this promise to can resolve it with whatever they want.
Run Code Online (Sandbox Code Playgroud)

使用示例 es6-promises

var foo = new Promise(function(resolve, reject) { resolve("foo") }) 
// encapsulation, no one is able to resolve this Promise but the originator of that Promise.
Run Code Online (Sandbox Code Playgroud)

如果您希望能够公开展示解决/拒绝功能,那么您需要展开承诺,例如此处.


Xot*_*750 23

如果节点使用的是Chrome 32使用的相同或更高版本的V8,则可能原生支持该节点.否则你需要加载'es6-shim'(我建议首先加载es5-shim)我不知道当前版本的节点正在使用哪个版本的V8.

在我的节点上安装

node --version
v0.10.24

node -e 'console.log(process.versions.v8);'
3.14.5.9
Run Code Online (Sandbox Code Playgroud)

Chrome V32使用的是哪个版本?(我使用的是Chromium v​​27),因此必须找到文档,但可能需要3.22.24.16

看着chrome://version/似乎给出了信息

Chromium    27.0.1453.93 (Developer Build 200836) Built from source for Fedora release 19 (Schrödinger’s Cat)
OS  Linux 
WebKit  537.36 (Unknown URL@0)
JavaScript  V8 3.17.6.14
Flash   11.2 r202
User Agent  Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Command Line     /usr/lib64/chromium-browser/chromium-browser --enable-plugins --enable-extensions --enable-user-scripts --enable-printing --enable-sync --auto-ssl-client-auth --flag-switches-begin --enable-sync-favicons --enable-full-history-sync --sync-keystore-encryption --flag-switches-end
Executable Path /usr/lib64/chromium-browser/chromium-browser
Profile Path    /home/graham/.config/chromium/Profile 1
Variations  b03ddc1f-2d9ef0cc
f9b252d0-fd526c81
ff3fc1a6-766fa2d
7f6da4bf-70d6abf1
75f7fb7e-611a9f49
262f996f-42d3ce07
24dca50e-455c9cca
ca65a9fe-91ac3782
3028188e-626278e
5e29d81-cf4f6ead
246fb659-6754d7b7
f296190c-72d8285f
4442aae2-4ad60575
75f0f0a0-a5822863
e2b18481-6e3b1976
e7e71889-e1cc0f14
Run Code Online (Sandbox Code Playgroud)

现在从Chrome安装

Google Chrome   32.0.1700.107 (Official Build 248368) 
OS  Linux 
Blink   537.36 (@165586)
JavaScript  V8 3.22.24.17
Flash   12.0.0.44
User Agent  Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
Command Line     /usr/bin/google-chrome-stable --flag-switches-begin --flag-switches-end
Executable Path /opt/google/chrome/google-chrome
Profile Path    /home/graham/.config/google-chrome/Default
Variations  b178bdde-887f97ea
24dca50e-837c4893
8d790604-9cb2a91c
5a3c10b5-e1cc0f14
244ca1ac-4ad60575
5e29d81-cf4f6ead
3ac60855-486e2a9c
246fb659-bd104136
f296190c-38939ee9
4442aae2-6e597ede
ed1d377-e1cc0f14
75f0f0a0-e1cc0f14
e2b18481-6e597ede
e7e71889-4ad60575
Run Code Online (Sandbox Code Playgroud)


lla*_*bda 6

我尝试使用--harmony标志的Node v0.11.12,它没有本机承诺.

v0.11.13和更高版本的--harmony标志确实有Promise.