如何在顶层使用async / await? [英] How can I use async/await at the top level?

查看:1172
本文介绍了如何在顶层使用async / await?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览async / await,经过几篇文章后,我决定自己测试一下。但是,我似乎无法解决为什么这不起作用:

I have been going over async/await and after going over several articles, I decided to test things myself. However, I can't seem to wrap my head around why this does not work:

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

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

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

The console outputs the following (node v8.6.0) :


> outside:[object Promise]

> outside: [object Promise]

>内部:嘿那里

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

Why does the log message inside the function execute afterwards? I thought the reason async/await was created was in order to perform synchronous execution using asynchronous tasks.

我是否可以使用函数内返回的值而不使用a .then() main()之后?

Is there a way could I use the value returned inside the function without using a .then() after main()?

推荐答案


我似乎无法理解为什么这不起作用。

I can't seem to wrap my head around why this does not work.

因为 main 返回一个承诺;所有 async 函数都可以。

Because main returns a promise; all async functions do.

在顶层,您必须:


  1. 使用从不拒绝的顶级 async 函数,或

使用然后 catch



#1 - 顶级 async 从不拒绝的功能



#1 - Top-level async function that never rejects

(async () => {
    try {
        var text = await main();
        console.log(text);
    } catch (e) {
        // Deal with the fact the chain failed
    }
})();

注意 catch ;你必须处理承诺拒绝/异步异常,因为没有别的东西;你没有调用者将它们传递给。如果您愿意,可以通过 catch 函数(而不是尝试 / <)来调用它。 code> catch 语法):

Notice the catch; you must handle promise rejections / async exceptions, since nothing else is going to; you have no caller to pass them on to. If you prefer, you could do that on the result of calling it via the catch function (rather than try/catch syntax):

(async () => {
    var text = await main();
    console.log(text);
})().catch(e => {
    // Deal with the fact the chain failed
});

...这样更简洁(因为这个原因,我喜欢它)。

...which is a bit more concise (I like it for that reason).

main()
    .then(text => {
        console.log(text);
    })
    .catch(err => {
        // Deal with the fact the chain failed
    });

如果出现错误,将调用 catch 处理程序发生在链中或然后处理程序中。 (确保 catch 处理程序不会抛出错误,因为没有注册任何错误来处理它们。)

The catch handler will be called if errors occur in the chain or in your then handler. (Be sure your catch handler doesn't throw errors, as nothing is registered to handle them.)

或两个参数然后

main().then(
    text => {
        console.log(text);
    },
    err => {
        // Deal with the fact the chain failed
    }
);

再次注意我们正在注册拒绝处理程序。但是在这种形式中,请确保您的然后回调都没有抛出任何错误,没有注册任何错误来处理它们。

Again notice we're registering a rejection handler. But in this form, be sure that neither of your then callbacks doesn't throw any errors, nothing is registered to handle them.

这篇关于如何在顶层使用async / await?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆