是否可以在Js中使用await而不使用异步 [英] Is it possible to use await without async in Js

查看:650
本文介绍了是否可以在Js中使用await而不使用异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Await是es7中的一个了不起的功能。

Await is a amazing feature in es7.

然而,每次我使用await时我发现我必须定义一个异步函数并调用此函数。

However,everytime I use await I found that I have to define a async function and call this function.

例如

    async function asy(){
        const [resCityGuess,resCityHot,resCityAll]=await Promise.all([
                        this.http.get('api/v1/cities?type=guess'),
                        this.http.get('api/v1/cities?type=hot'),
                        this.http.get('api/v1/cities?type=group')
        ])
        this.cityGuessName=resCityGuess.data.name;
        this.cityGuessId=resCityGuess.data.id;
        this.cityHot=resCityHot.data;
        this.cityAll=resCityAll.data;
    }
    asy.apply(this);

我想要的是使用等待没有异步功能,例如

What I want is use await without async function such as

        // the async function definition is deleted
        const [resCityGuess,resCityHot,resCityAll]=await Promise.all([
                        this.http.get('api/v1/cities?type=guess'),
                        this.http.get('api/v1/cities?type=hot'),
                        this.http.get('api/v1/cities?type=group')
        ])
        this.cityGuessName=resCityGuess.data.name;
        this.cityGuessId=resCityGuess.data.id;
        this.cityHot=resCityHot.data;
        this.cityAll=resCityAll.data;
        // without call fn

我认为定义函数fn并调用此fn重复有时我想知道是否有可能优化这种情况?

I think define the function fn and call this fn is repeated sometimes so I want to know is it possible to optimize the situation?

我可以使用await而不是异步吗?

Can I use await without async?

非常感谢你!

推荐答案

否。 await 运算符仅在 async 函数中有意义。

No. The await operator only makes sense in an async function.

edit - 详细说明:整个 async 等待交易可以被认为是像LISP宏一样。该语法的作用是通知语言解释系统正在发生的事情,以便实际将周围代码的转换合成为基于Promise的回调请求序列。

edit — to elaborate: the whole async and await deal can be thought of as being like a LISP macro. What that syntax does is inform the language interpretation system of what's going on, so that it can in effect synthesize a transformation of the surrounding code into a Promise-based sequence of callback requests.

因此,使用语法是编写显式Promise内容的隐含快捷方式,调用 .then()等。运行时必须知道函数是 async 因为它知道函数内的 async 表达式需要转换通过生成器机制返回Promises。并且,由于重叠原因,函数声明上的 async 装饰告诉语言这实际上是一个返回Promise的函数,并且它需要处理它。

Thus using the syntax is an implicit short-cut to coding up the explicit Promise stuff, with calls to .then() etc. The runtime has to know that a function is async because then it knows that async expressions inside the function need to be transformed to return Promises via a generator mechanism. And, for overlapping reasons, the async decoration on the function declaration tells the language that this is really a function that returns a Promise and that it needs to deal with that.

所以,这很复杂。改进和扩展JavaScript的过程必须考虑到世界上存在难以想象的大量JavaScript代码的事实,因此几乎在所有情况下,没有新功能可以导致自2002年以来未触及的页面失败。

So, it's complicated. The process of improving and extending JavaScript has to account for the fact that there's an unimaginably massive amount of JavaScript code out in the world, and so in almost all cases no new feature can cause a page untouched since 2002 to fail.

这篇关于是否可以在Js中使用await而不使用异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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