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 trialLuis Walderdorff
Full Stack JavaScript Techdegree Graduate 27,070 PointsI don't understand the async keyword.
Would it transform the getUsers function into a promise if it wasn't one?
1 Answer
Steven Parker
231,236 PointsYou'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);
}
Luis Walderdorff
Full Stack JavaScript Techdegree Graduate 27,070 PointsLuis Walderdorff
Full Stack JavaScript Techdegree Graduate 27,070 PointsAh okay, thanks!
JASON LEE
17,352 PointsJASON LEE
17,352 PointsShouldn't the
return Promise.resolve(1);
in this example bereturn new Promise.resolve(1)
? Just trying to understand how it all ties together.Steven Parker
231,236 PointsSteven Parker
231,236 PointsThis 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.