Helenr
2019-10-19 16:21:00
我正在嘗試將Node 6.2.1版與一些代碼一起使用。計(jì)劃將大多數(shù)面向超級(jí)回調(diào)的代碼遷移到看起來(lái)更干凈甚至性能更好的代碼上。我不知道為什么,當(dāng)我嘗試執(zhí)行節(jié)點(diǎn)代碼時(shí),終端拋出錯(cuò)誤。helloz.js(async function testingAsyncAwait() { await console.log("Print me!");})();日志BOZZMOB-M-T0HZ:rest bozzmob$ node helloz.js /Users/bozzmob/Documents/work/nextgennms/rest/helloz.js:1(function (exports, require, module, __filename, __dirname) { (async function testingAsyncAwait() { ^^^^^^^^SyntaxError: Unexpected token function at Object.exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:513:28) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Function.Module.runMain (module.js:575:10) at startup (node.js:160:18) at node.js:456:3BOZZMOB-M-T0HZ:rest bozzmob$ node -vv6.2.1我想念什么?請(qǐng)給我一些啟示。更新1:我嘗試按照Quentin的建議使用Babel,但是,我仍然收到以下錯(cuò)誤。更新的代碼-require("babel-core/register");require("babel-polyfill"); (async function testingAsyncAwait() { await console.log("Print me!"); })();日志BOZZMOB-M-T0HZ:rest bozzmob$ babel helloz.js > helloz.trans.jsSyntaxError: helloz.js: Unexpected token (3:7) 1 | require("babel-polyfill"); 2 | > 3 | (async function testingAsyncAwait() { | ^ 4 | await console.log("Print me!"); 5 | })();
3 回答

阿晨1998
TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
早于7.6版的Node版本不支持異步功能。
如果您使用的是舊版本,則需要將代碼(例如,使用Babel)轉(zhuǎn)換為Node可以理解的JS版本。
就是說(shuō),當(dāng)前(2018年)的Node.js LTS版本是8.x,因此,如果您使用的是較早版本,則應(yīng)強(qiáng)烈考慮升級(jí)。

紫衣仙女
TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
Node.JS當(dāng)前不完全支持ES6,因此您可以使用asyncawait模塊或使用Bable對(duì)其進(jìn)行轉(zhuǎn)換。
安裝
npm install --save asyncawait
helloz.js
var async = require('asyncawait/async');
var await = require('asyncawait/await');
(async (function testingAsyncAwait() {
await (console.log("Print me!"));
}))();
添加回答
舉報(bào)
0/150
提交
取消