节点 7.10 - 它支持等待吗?
node 7.10 - does it support await?
我在节点 7.10 上:
$ node --version
v7.10.0
我以为它支持await
。
let result = await Weather.findOne(options, function(err, weather) {
if (err) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Error occurs: ' + err);
}
if (weather) {
res.set('Content-Type', 'application/json');
return res.status(200).send(key + ' already exist.');
}
return weather;
});
console.log(result);
错误信息:
let result = await Weather.findOne(options, function(err, weather) {
^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/html/projects/citizen-sense-dustbox-data-streams/es5/v4/app.js:33:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/html/projects/citizen-sense-dustbox-data-streams/es5/v4/bin/www:7:11)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
node 7.10 好像不支持?还是我做错了什么?
有什么想法吗?
来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await:
The await operator is used to wait for a Promise. It can only be used inside an async function.
async function doAwait() {
let result = await Weather.findOne(options, function(err, weather) {
if (err) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Error occurs: ' + err);
}
if (weather) {
res.set('Content-Type', 'application/json');
return res.status(200).send(key + ' already exist.');
}
return weather;
});
console.log(result);
}
我有 v7.10.0,await/async 绝对支持该版本。我认为它是在 7.4 中添加的,但不确定。
ubuntu@ubuntu:~$ node --version
v7.10.0
当我运行:
const getDate = async () => {
return new Date()
}
const printDate = async () => {
const date = await getDate()
return date
}
printDate().then(console.log).catch(console.error)
我得到:
2017-07-04T09:04:27.311Z
编辑:
仅供参考,async/await 实际上并没有进入 ES7。不过,它很可能会出现在今年的 ECMAScript 版本中。 https://github.com/tc39/proposals/blob/master/finished-proposals.md
我在节点 7.10 上:
$ node --version
v7.10.0
我以为它支持await
。
let result = await Weather.findOne(options, function(err, weather) {
if (err) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Error occurs: ' + err);
}
if (weather) {
res.set('Content-Type', 'application/json');
return res.status(200).send(key + ' already exist.');
}
return weather;
});
console.log(result);
错误信息:
let result = await Weather.findOne(options, function(err, weather) {
^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/html/projects/citizen-sense-dustbox-data-streams/es5/v4/app.js:33:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/html/projects/citizen-sense-dustbox-data-streams/es5/v4/bin/www:7:11)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
node 7.10 好像不支持?还是我做错了什么?
有什么想法吗?
来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await:
The await operator is used to wait for a Promise. It can only be used inside an async function.
async function doAwait() {
let result = await Weather.findOne(options, function(err, weather) {
if (err) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Error occurs: ' + err);
}
if (weather) {
res.set('Content-Type', 'application/json');
return res.status(200).send(key + ' already exist.');
}
return weather;
});
console.log(result);
}
我有 v7.10.0,await/async 绝对支持该版本。我认为它是在 7.4 中添加的,但不确定。
ubuntu@ubuntu:~$ node --version
v7.10.0
当我运行:
const getDate = async () => {
return new Date()
}
const printDate = async () => {
const date = await getDate()
return date
}
printDate().then(console.log).catch(console.error)
我得到:
2017-07-04T09:04:27.311Z
编辑: 仅供参考,async/await 实际上并没有进入 ES7。不过,它很可能会出现在今年的 ECMAScript 版本中。 https://github.com/tc39/proposals/blob/master/finished-proposals.md