r/javascript • • Apr 23 '19

Introducing Node.js 12

https://medium.com/@nodejs/introducing-node-js-12-76c41a1b3f3f
333 Upvotes

85 comments sorted by

View all comments

Show parent comments

31

u/blood_bender Apr 23 '19

Basically async marks a function as required to return a Promise. So the earlier approach to the v8 implementation would take whatever value was returned from the function, wrap it in a promise, and return that instead.

Now it looks to see if what's being returned is a native Promise, and if it is, just returns that instead of wrapping it (it still needs to wrap a value if you're not returning a promise, but that's pretty uncommon).

Seems like a naive approach the first time around, it's a little unclear to me why they didn't do that up front, but oh well.

3

u/[deleted] Apr 24 '19

Not manually returning a promise in an async function is uncommon? I almost always write my async functions that way.

3

u/redmorphium Apr 24 '19

Correct me if my understanding is wrong, but let's say you have:


async function foo() {

  await bar();

  return 1;

}

This is equivalent to:

async function foo() {

  return bar().then(() => 1);

}

So unless if you're writing async functions that don't have any awaits in them, then async functions should always effectively return promises?

4

u/braindeadTank Apr 24 '19

Async function always return a promise, no ifs, buts or unlesses. If you don't await anything, it will just be resolved right away.