0

According MDN Async return value: A Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function.

However,function no.1 returned promise object with a returned value as a promiseResult. function no.2 returned promise object with a "undefined" as a promiseResult.

    async function foo1(){ return 1 }   
    console.log(foo1())   // Promise{promiseResult:1}
    
    async function foo2(){
        setTimeout(()=>{return 1},1000) 
    }
    console.log(foo2())  //Promise{promiseResult:undefined}

I wonder why, second function won't return a value 1 wrappd with promsie object. Is that just ES6 stardard ??

feeco
  • 75
  • 4
  • 2
    Function 2 does not have a `return` statement in itself, so it implicitly produces `undefined`. This is *not* an ES6 standard, functions have *never* returned the result of a function declared inside. `return` has *always* only mattered for the current function. – VLAZ Aug 13 '21 at 07:04
  • thanks so much, VLAZ. you answered my question right to the point. I don't know how to take your answer as formal answer. will check it out and mark it as taken. thanks again for the help. – feeco Aug 13 '21 at 07:31

1 Answers1

0

Function 2 does not have a return statement in itself, so it implicitly produces undefined. This is not an ES6 standard, functions have never returned the result of a function declared inside. return has always only mattered for the current function. – VLAZ

feeco
  • 75
  • 4