Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Asynchronous Code in Express Asynchronous Code in Express Refactor Using Async/Await

1 Answer

Steven Parker
Steven Parker
229,771 Points

You're right, even If a function returns a non-promise value, prepending the function definition with the “async” keyword directs JavaScript to automatically wrap that value in a resolved promise. For example:

async function f() {          // this function is the same...
  return 1;
}
async function f() {          // ...as this one
  return Promise.resolve(1);
}

Shouldn't the return Promise.resolve(1); in this example be return new Promise.resolve(1)? Just trying to understand how it all ties together.

Steven Parker
Steven Parker
229,771 Points

This code isn't invoking a constructor directly, it's calling a class method. The class method "resolve" creates a new promise internally and returns it.