问题描述
当前版本的 Node.js 是否有对 promise 的原生支持?
Is there native support for promises in current versions of Node.js?
Node.js 使用 V8 引擎.Chrome 也使用了这个 JavaScript 引擎,Chrome 32 原生支持 promises.但我似乎无法得到在 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.
我在 Chrome 32 中尝试了以下代码并且它有效.
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 );
});
然而,当我在 Node.js 中尝试相同的代码时,我得到:
However, when I try this same code in Node.js, I get:
var promise = new Promise(function(resolve, reject) {
^
ReferenceError: Promise is not defined
此代码来自优秀教程:
http://www.html5rocks.com/en/tutorials/es6/promises/
推荐答案
如果 node 使用与 Chrome 32 相同或更高版本的 V8,那么它很可能是本机支持的.否则你将需要加载 'es6-shim'(我建议加载 es5-shim 首先)我不知道当前版本的 node 正在使用哪个版本的 V8.
If node is using the same or later version of V8 that Chrome 32 uses then it is likely natively supported. Otherwise you will need to load 'es6-shim' (I recommend loading es5-shim first) I have no idea which version of V8 the current release of node is using.
在我的节点上安装
node --version
v0.10.24
node -e 'console.log(process.versions.v8);'
3.14.5.9
Chrome V32 使用的是什么版本?(我使用的是 Chromium v27),所以必须找到文档,但可能是 3.22.24.16
What version is Chrome V32 using? (I am using Chromium v27), so would have to find documentation, but possibly 3.22.24.16
查看 chrome://version/
似乎给出了信息
Looking in chrome://version/
seems to give the information
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
现在从 Chrome 安装
And now from a Chrome install
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
这篇关于Node.js 中对 Promise 的原生支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!