Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use await without async in Js

Await is a amazing feature in es7.

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

Such as

    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

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?

Can I use await without async?

Thank you so much!

like image 668
曾志强 Avatar asked May 07 '17 14:05

曾志强


People also ask

Can we use await without putting async at the beginning of a function?

await can only be used inside async function.

What if await is not used with async?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Can you use await on a non promise?

This rule applies when the await operator is used on a non-Promise value. await operator pauses the execution of the current async function until the operand Promise is resolved.


2 Answers

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

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.

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 await 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.

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.

edit — Now, here in 2021, there are rules for how an await call works in the outer level of a module. It's not quite the same as how it works in an async function situation, but it's similar.

like image 172
Pointy Avatar answered Nov 03 '22 01:11

Pointy


It's proposed to ECMAScript.

Chrome/Chromium (and anything with an up-to-date V8-based JS engine) has a working implementation that appears to be compliant with the specification.

The proposal itself is at stage 3.

More info:

https://github.com/tc39/proposal-top-level-await

https://v8.dev/features/top-level-await

like image 42
Chumpocomon Avatar answered Nov 03 '22 01:11

Chumpocomon