【问题标题】:node 7.10 - does it support await?节点 7.10 - 它支持等待吗?
【发布时间】:2017-12-07 15:37:28
【问题描述】:

我在节点 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 好像不支持?还是我做错了什么?

有什么想法吗?

【问题讨论】:

  • 根据node.green支持
  • async/await 不是 ES7 (ES2016) 的一部分!它是今年发布的 ES2018 的一部分。

标签: node.js express mongoose async-await ecmascript-2017


【解决方案1】:

来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

await 运算符用于等待 Promise。 只能在异步函数中使用

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);
}

【讨论】:

    【解决方案2】:

    我有 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

    【讨论】:

    • 谢谢,但我不明白 - console.logconsole.error 来自哪里?
    • 它们内置在 nodejs 和几乎任何浏览器中。
    • “它很可能会出现在今年的 ECMAScript 版本中” 今年的修订版已经发布,是的,它确实出现了 ;)
    • 哦,我一定错过了。在维基百科上,它仍然显示not finished yet。什么时候出版的?
    猜你喜欢
    • 2019-12-10
    • 2021-05-07
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 2016-01-06
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多