我已经阅读了async/await,在阅读了几篇文章之后,我决定自己测试一下。然而,我似乎不明白为什么这行不通:

async function main() {  
    var value = await Promise.resolve('Hey there');
    console.log('inside: ' + value);
    return value;
}

var text = main();  
console.log('outside: ' + text);

控制台输出以下内容(节点v8.6.0):

> outside: [object Promise] > inside:嘿,大家好

为什么函数内部的日志消息之后执行?我认为创建async/await的原因是为了使用异步任务执行同步执行。

是否有一种方法可以使用函数内部返回的值,而不使用main()后的.then() ?


当前回答

因为main()是异步运行的,所以它返回一个承诺。你必须在then()方法中获取结果。因为then()也返回promise,所以必须调用process.exit()来结束程序。

main()
   .then(
      (text) => { console.log('outside: ' + text) },
      (err)  => { console.log(err) }
   )
   .then(() => { process.exit() } )

其他回答

你需要在package.json中添加类型 “类型”:“模块” 你可以出发了。 从'axios'导入axios; Const res = await axios.get('https://api.github.com/users/wesbos'); console.log (res.data);

记住,如果你改变了文档的类型,那么你必须以ES6的方式编写代码。

节点, 你可以在REPL中运行node——experimental-repl-await。我不太确定脚本。

Deno - Deno已经内置了它。

对于浏览器,你需要添加type="module"

没有类型=“模块”

< >脚本 Const response = await fetch('https://jsonplaceholder.typicode.com/users'); Const users = await sp.json(); console.log(用户) > < /脚本

类型=“模块”

<!——script type="module" src=" wait.js"——> .js . <脚本类型=“模块”> Const response = await fetch('https://jsonplaceholder.typicode.com/users'); Const users = await sp.json(); console.log(用户) > < /脚本

在当前答案的基础上提供一些进一步的信息:

node.js文件的内容目前以类似字符串的方式连接起来,以形成一个函数体。

例如,如果你有一个文件test.js:

// Amazing test file!
console.log('Test!');

然后node.js会秘密地连接一个函数,看起来像这样:

function(require, __dirname, ... perhaps more top-level properties) {
  // Amazing test file!
  console.log('Test!');
}

需要注意的主要事情是,得到的函数不是一个异步函数。所以你不能在里面直接使用await这个词!

但是如果你需要使用这个文件中的承诺,那么有两种可能的方法:

不要在函数中直接使用await 完全不要使用await

选项1要求我们创建一个新的作用域(这个作用域可以是异步的,因为我们可以控制它):

// Amazing test file!
// Create a new async function (a new scope) and immediately call it!
(async () => {
  await new Promise(...);
  console.log('Test!');
})();

选项2要求我们使用面向对象的承诺API(使用承诺的不太漂亮但功能相同的范例)

// Amazing test file!
// Create some sort of promise...
let myPromise = new Promise(...);

// Now use the object-oriented API
myPromise.then(() => console.log('Test!'));

看到节点添加对顶级await的支持会很有趣!

其他解决方案缺乏POSIX兼容性的一些重要细节:

你需要…

成功时报告0退出状态,失败时报告非0退出状态。 向标准输出流发出错误。

#!/usr/bin/env node

async function main() {
 // ... await stuff ... 
}

// POSIX compliant apps should report an exit status
main()
    .then(() => {
        process.exit(0);
    })
    .catch(err => {
        console.error(err); // Writes to stderr
        process.exit(1);
    });

如果使用command等命令行解析器,则可能不需要main()。

例子:

#!/usr/bin/env node

import commander from 'commander'

const program = new commander.Command();

program
  .version("0.0.1")
  .command("some-cmd")
  .arguments("<my-arg1>")
  .action(async (arg1: string) => {
    // run some async action
  });

program.parseAsync(process.argv)
  .then(() => {
    process.exit(0)
  })
  .catch(err => {
    console.error(err.message || err);
    if (err.stack) console.error(err.stack);
    process.exit(1);
  });