Async function without await in Javascript

后端 未结 4 895
南旧
南旧 2020-11-29 03:36

I have two functions a and b that are asynchronous, the former without await and the later with await. They both log some

相关标签:
4条回答
  • 2020-11-29 04:11

    mozilla doc:

    An async function can contain an await expression, that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

    As you assumed, if no await is present the execution is not paused and your code will then be executed in a non-blocking manner.

    0 讨论(0)
  • 2020-11-29 04:22

    The function is executed the same with or without await. What await does is automatically wait for the promise that's returned by the function to be resolved.

    await timeout(1000);
    more code here;
    

    is roughly equivalent to:

    timeout(1000).then(function() {
        more code here;
    });
    

    The async function declaration simply makes the function automatically return a promise that's resolved when the function returns.

    0 讨论(0)
  • 2020-11-29 04:26

    Everything is synchronous until a Javascript asynchronous function is executed. In using async-await await is asynchronous and everything after await is placed in event queue. Similar to .then().

    To better explain take this example:

    function main() {
      return new Promise( resolve => {
        console.log(3);
        resolve(4);
        console.log(5);
      });
    }
    
    async function f(){
        console.log(2);
        let r = await main();
        console.log(r);
    }
    
    console.log(1);
    f();
    console.log(6);
    

    As await is asynchronous and rest all is synchronous including promise thus output is

    1
    2
    3
    5
    6
    // Async happened, await for main()
    4
    

    Similar behavior of main() is without promise too:

    function main() {
        console.log(3);
        return 4;
    }
    
    async function f(){
        console.log(2);
        let r = await main();
        console.log(r);
    }
    
    console.log(1);
    f();
    console.log(5);
    

    Output:

    1
    2
    3
    5
    // Asynchronous happened, await for main()
    4
    

    Just removing await will make whole async function synchronous which it is.

    function main() {
        console.log(3);
        return 4;
    }
    
    async function f(){
        console.log(2);
        let r = main();
        console.log(r);
    }
    
    console.log(1);
    f();
    console.log(5);
    

    Output:

    1
    2
    3
    4
    5
    
    0 讨论(0)
  • 2020-11-29 04:27

    As other answers say/indicate: an async function just runs on spot until it encounters an await - if there is no await, it runs completely.

    What may be worth adding that async unconditionally makes your result a Promise. So if you return something, there is a difference already and you simply can not get the result without returning to the JS engine first (similarly to event handling):

    async function four(){
      console.log("  I am four");
      return 4;
    }
    console.log(1);
    let result=four();
    console.log(2,"It is not four:",result,"Is it a promise ?", result instanceof Promise);
    result.then(function(x){console.log(x,"(from then)");});
    console.log(3);

    0 讨论(0)
提交回复
热议问题