ChatGPT解决这个技术问题 Extra ChatGPT

Difference between `return await promise` and `return promise`

Given the code samples below, is there any difference in behavior, and, if so, what are those differences?

return await promise

async function delay1Second() {
  return (await delay(1000));
}

return promise

async function delay1Second() {
  return delay(1000);
}

As I understand it, the first would have error-handling within the async function, and errors would bubble out of the async function's Promise. However, the second would require one less tick. Is this correct?

This snippet is just a common function to return a Promise for reference.

function delay(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}
Yeah I edited my question because you misunderstood my meaning and it didn't really answer what I was wondering.
@PitaJ: I believe you meant to remove the async from your second (return promise) sample.
@StephenCleary nope. I meant for this. Imagine there are other await calls, etc before the return.
jakearchibald.com/2017/await-vs-return-vs-return-await is a nice article that summarises the differences
@StephenCleary, I stumbled upon this and first thought exactly the same, a promise that is resolved with a promise doesn't make sense here. But as it turns, promise.then(() => nestedPromise) would flatten and "follow" the nestedPromise. Interesting how it's different from nested tasks in C# where we'd have to Unwrap it. On a side note, it appears that await somePromise calls Promise.resolve(somePromise).then, rather than just somePromise.then, with some interesting semantic differences.

D
Denis Washington

Most of the time, there is no observable difference between return and return await. Both versions of delay1Second have the exact same observable behavior (but depending on the implementation, the return await version might use slightly more memory because an intermediate Promise object might be created).

However, as @PitaJ pointed out, there is one case where there is a difference: if the return or return await is nested in a try-catch block. Consider this example

async function rejectionWithReturnAwait () {
  try {
    return await Promise.reject(new Error())
  } catch (e) {
    return 'Saved!'
  }
}

async function rejectionWithReturn () {
  try {
    return Promise.reject(new Error())
  } catch (e) {
    return 'Saved!'
  }
}

In the first version, the async function awaits the rejected promise before returning its result, which causes the rejection to be turned into an exception and the catch clause to be reached; the function will thus return a promise resolving to the string "Saved!".

The second version of the function, however, does return the rejected promise directly without awaiting it within the async function, which means that the catch case is not called and the caller gets the rejection instead.


Maybe also mention that the stack trace would be different (even without a try/catch)? I think that's the issue people run into the most often in this example :]
i have found in one scenario, that using return new Promise(function(resolve, reject) { }) within a for...of loop and then calling resolve() within the loop after a pipe() does not pause program execution till pipe has completed, as desired, however using await new Promise(...) does. is the latter even valid/correct syntax? is it ‘shorthand’ for return await new Promise(...)? could you help me understand why the latter works and the former does not? for context, the scenario is in solution 02 of this answer
c
chharvey

As other answers mentioned, there is likely a slight performance benefit when letting the promise bubble up by returning it directly — simply because you don’t have to await the result first and then wrap it with another promise again. However, no one has talked about tail call optimization yet.

Tail call optimization, or “proper tail calls”, is a technique that the interpreter uses to optimize the call stack. Currently, not many runtimes support it yet — even though it’s technically part of the ES6 Standard — but it’s possible support might be added in the future, so you can prepare for that by writing good code in the present.

In a nutshell, TCO (or PTC) optimizes the call stack by not opening a new frame for a function that is directly returned by another function. Instead, it reuses the same frame.

async function delay1Second() {
  return delay(1000);
}

Since delay() is directly returned by delay1Second(), runtimes supporting PTC will first open a frame for delay1Second() (the outer function), but then instead of opening another frame for delay() (the inner function), it will just reuse the same frame that was opened for the outer function. This optimizes the stack because it can prevent a stack overflow (hehe) with very large recursive functions, e.g., fibonacci(5e+25). Essentially it becomes a loop, which is much faster.

PTC is only enabled when the inner function is directly returned. It’s not used when the result of the function is altered before it is returned, for example, if you had return (delay(1000) || null), or return await delay(1000).

But like I said, most runtimes and browsers don’t support PTC yet, so it probably doesn’t make a huge difference now, but it couldn’t hurt to future-proof your code.

Read more in this question: Node.js: Are there optimizations for tail calls in async functions?


R
Ragtime

Noticeable difference: Promise rejection gets handled at different places

return somePromise will pass somePromise to the call site, and await somePromise to settle at call site (if there is any). Therefore, if somePromise is rejected, it will not be handled by the local catch block, but the call site's catch block.

async function foo () { try { return Promise.reject(); } catch (e) { console.log('IN'); } } (async function main () { try { let a = await foo(); } catch (e) { console.log('OUT'); } })(); // 'OUT'

return await somePromise will first await somePromise to settle locally. Therefore, the value or Exception will first be handled locally. => Local catch block will be executed if somePromise is rejected.

async function foo () { try { return await Promise.reject(); } catch (e) { console.log('IN'); } } (async function main () { try { let a = await foo(); } catch (e) { console.log('OUT'); } })(); // 'IN'

Reason: return await Promise awaits both locally and outside, return Promise awaits only outside

Detailed Steps:

return Promise

async function delay1Second() {
  return delay(1000);
}

call delay1Second();

const result = await delay1Second();

Inside delay1Second(), function delay(1000) returns a promise immediately with [[PromiseStatus]]: 'pending. Let's call it delayPromise.

async function delay1Second() {
  return delayPromise;
// delayPromise.[[PromiseStatus]]: 'pending'
// delayPromise.[[PromiseValue]]: undefined
}

Async functions will wrap their return value inside Promise.resolve()(Source). Because delay1Second is an async function, we have:

const result = await Promise.resolve(delayPromise); 
// delayPromise.[[PromiseStatus]]: 'pending'
// delayPromise.[[PromiseValue]]: undefined

Promise.resolve(delayPromise) returns delayPromise without doing anything because the input is already a promise (see MDN Promise.resolve):

const result = await delayPromise; 
// delayPromise.[[PromiseStatus]]: 'pending'
// delayPromise.[[PromiseValue]]: undefined

await waits until the delayPromise is settled.

IF delayPromise is fulfilled with PromiseValue=1:

const result = 1; 

ELSE is delayPromise is rejected:

// jump to catch block if there is any

return await Promise

async function delay1Second() {
  return await delay(1000);
}

call delay1Second();

const result = await delay1Second();

Inside delay1Second(), function delay(1000) returns a promise immediately with [[PromiseStatus]]: 'pending. Let's call it delayPromise.

async function delay1Second() {
  return await delayPromise;
// delayPromise.[[PromiseStatus]]: 'pending'
// delayPromise.[[PromiseValue]]: undefined
}

Local await will wait until delayPromise gets settled.

Case 1: delayPromise is fulfilled with PromiseValue=1:

async function delay1Second() {
  return 1;
}
const result = await Promise.resolve(1); // let's call it "newPromise"
const result = await newPromise; 
// newPromise.[[PromiseStatus]]: 'resolved'
// newPromise.[[PromiseValue]]: 1
const result = 1; 

Case 2: delayPromise is rejected:

// jump to catch block inside `delay1Second` if there is any
// let's say a value -1 is returned in the end
const result = await Promise.resolve(-1); // call it newPromise
const result = await newPromise;
// newPromise.[[PromiseStatus]]: 'resolved'
// newPromise.[[PromiseValue]]: -1
const result = -1;

Glossary:

Settle: Promise.[[PromiseStatus]] changes from pending to resolved or rejected


Explained beautifully! The step by step wrapping and unwrapping of promises made the difference crystal clear. One of the important takeaways from this is the value returned by Promise.resolve when a promise is passed. I had initially thought that it would return a resolved promise but no, it returns back the promise as is.
n
nrabinowitz

This is a hard question to answer, because it depends in practice on how your transpiler (probably babel) actually renders async/await. The things that are clear regardless:

Both implementations should behave the same, though the first implementation may have one less Promise in the chain.

Especially if you drop the unnecessary await, the second version would not require any extra code from the transpiler, while the first one does.

So from a code performance and debugging perspective, the second version is preferable, though only very slightly so, while the first version has a slight legibility benefit, in that it clearly indicates that it returns a promise.


Why would the functions behave the same? The first returns a resolved value (undefined) and the second returns a Promise.
@Amit both functions return a Promise
Ack. This is why I can't stand async/await - I find it much harder to reason about. @PitaJ is correct, both functions return a Promise.
What if I were to surround the body of both async functions with a try-catch? In the return promise case, any rejection would not be caught, correct, whereas, in thereturn await promise case, it would be, right?
Both return a Promise, but the first "promises" a primitive value, and the second "promises" a Promise. If you await each of these at some call site, the result will be very different.
D
David Dehghan

Here is a typescript example that you can run and convince yourself that you need that "return await"

async function test() { try { return await throwErr(); // this is correct // return throwErr(); // this will prevent inner catch to ever to be reached } catch (err) { console.log("inner catch is reached") return } } const throwErr = async () => { throw("Fake error") } void test().then(() => { console.log("done") }).catch(e => { console.log("outer catch is reached") });


I agree. It is so sad to see some respected js magicians advocate for the opposite here on StackOverflow.
T
Tamas Hegedus

In our project, we decided to always use 'return await'. The argument is that "the risk of forgetting to add the 'await' when later on a try-catch block is put around the return expression justifies having the redundant 'await' now."


I 100% agree. Also explaining to new joiners that always use await when calling async functions, except when it is immediately returned, except when it is in a try-catch is just ridiculous.
C
Carlos Terrazas

here i leave some code practical for you can undertand it the diferrence

 let x = async function () {
  return new Promise((res, rej) => {
    setTimeout(async function () {
      console.log("finished 1");
      return await new Promise((resolve, reject) => { // delete the return and you will see the difference
        setTimeout(function () {
          resolve("woo2");
          console.log("finished 2");
        }, 5000);
      });
      res("woo1");
    }, 3000);
  });
};

(async function () {
  var counter = 0;
  const a = setInterval(function () { // counter for every second, this is just to see the precision and understand the code
    if (counter == 7) {
      clearInterval(a);
    }

    console.log(counter);
    counter = counter + 1;
  }, 1000);
  console.time("time1");
  console.log("hello i starting first of all");
  await x();
  console.log("more code...");
  console.timeEnd("time1");
})();

the function "x" just is a function async than it have other fucn if will delete the return it print "more code..."

the variable x is just an asynchronous function that in turn has another asynchronous function, in the main of the code we invoke a wait to call the function of the variable x, when it completes it follows the sequence of the code, that would be normal for "async / await ", but inside the x function there is another asynchronous function, and this returns a promise or returns a" promise "it will stay inside the x function, forgetting the main code, that is, it will not print the" console.log ("more code .. "), on the other hand if we put" await "it will wait for every function that completes and finally follows the normal sequence of the main code.

below the "console.log (" finished 1 "delete the" return ", you will see the behavior.


While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.