Async await strange behaviour with functions

前端 未结 2 2018
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 22:16

I have following asynchronous code example:

// Functions

function getSomePromise() {
    let a = new Promise((resolve, reject) => {    
        setTimeou         


        
相关标签:
2条回答
  • 2020-12-21 22:49

    test2:

    your test2 is not async until you make it async. You've wrote synchronous code inside test2.they are the console.log. Only async code inside test2 is call to promise.Let's break it down

    async function test2() {
        for(let i=0; i<5; i++) {
            someWrapper(i);                
        }    
    }
    

    above code fires someWrapper() 5 times sequentially.so it write the first sync code which is console.log('A'+i) 5 times in a row in console.

    then each someWrapper() waits for async promise to return parallelly.after each promise resolved it prints 'Inside promise'. until the promise resolves, the execution halts and can not proceed to next step

    then, after resolving the promise, it prints out the second sync code which is console.log('B'+i) in console

    test1:

    test1 will behave differently than test2.Let's break it down

    async function test1() {
        for(let i=0; i<5; i++) {
            // body copy-pasted of someWrapper function:
            console.log('A: '+ i);
            await getSomePromise();
            console.log('B: ' + i);
        }    
    }
    

    the main distinction is you are awaiting inside for loop. So this will literally pause the loop which was not the case for test1

    so for each iteration it will print console.log('A'+i)

    then pause the iteration for await getSomePromise()

    when the promise return will print 'Inside Promise'

    then print console.log('B'+i)

    then continue next iteration.

    0 讨论(0)
  • 2020-12-21 23:04

    Looking at the comments

    @HMR - hm... I not understand - in question example there is async function someWrapper() but that function don't return anything (it even doesn't have return statement (!) ) - can you explain what do you mean by async functions immediately return a promise? - Kamil Kielczewski

    it seems you don't understand the async await. I usually advice people to lay off await until you understand promises. However in next comment under question I give you the answer:

    someWrapper will immediately return a promise that resolves to undefined. The await only "waits" in the someWrapper function but the function calling someWrapper will immediately receive a promise that resolves in undefined. Functions always return something, if you don't in code then it will return undefined. If it's an async function without a return then it'll return a promise that resolves in undefined - HMR.

    Await is syntax sugar (nicer looking code) for promises and doesn't actually wait for anything.

    Maybe the following code clears things up:

    var test = async () => {
       await 22;//doesn't even matter if value is promise
       console.log("after wait");
    }
    var result = test();
    console.log("outside test we don't wait for anything",result);

    If you don't understand why the output of that code is:

    outside test we don't wait for anything Promise {< pending >}

    after wait

    Then I'd advice you to use just promises, until you do.

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