在当前版本的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)
这段代码来自优秀的教程:
我正在使用基于维基媒体引擎的维基,即维基百科使用的相同维基技术。
如何使用 wiki 语言编写一个链接,以便在新窗口或选项卡中打开目标 URL。本质上,我想要相当于以下内容的维基语言:
<a href="http://www.google.com" target="new">Google</a>
Run Code Online (Sandbox Code Playgroud)