0
async function someAsyncFunc() {
    const [user, categories] = await Promise.all([
        asyncGetUser(),
        asyncGetCategories()
    ]);
    const mapping = await asyncMapUserWithCategory(user, categories);
};

To get the mapping, I need to get user and categories first. These come from a DB, so I use Promise.all to fetch them at once and then feed them to asyncMapUserWithCategory() (not sure why I had put await before that, but nevermind). I am pretty sure asyncGetUser() and asyncGetCategories() must return promises. EDIT: .., or a value. (sorry, forgot about this)

In case I did the same without Promise.all (below, I suppose slower) would the await-ed function also need to return promises for this to work?

async function someAsyncFunc() {
    const user = await asyncGetUser();
    const categories = await asyncGetCategories();
    const mapping = await asyncMapUserWithCategory(user, categories);
};
marko-36
  • 1,309
  • 3
  • 23
  • 38
  • async function get automatically wrapped in promises if they don't return a promise Try the following: `await (async function() {return 1})(); (async function() {return 1})().then(x => console.log(x))`; – Ruan Mendes May 14 '19 at 19:52

5 Answers5

0

Awaiting something that isn't a promise won't be a syntax error or anything like that. However, it also won't do anything of note. You havn't told it what to wait for, and so it will resume immediately.

My recommendation: If the code is synchronous, leave off the await to make it more obvious that it's synchronous. If it's asynchronous, then return a promise and await that promise.

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
0

I am pretty sure asyncGetUser() and asyncGetCategories() must return promises.

No, they don't need to.

Would the await-ed function also need to return promises for this to work?

No, it's the same for await.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Answer:

No.


Why?

You can return a normal value OR a Promise. If it's a normal value it is converted into a resolved Promise before being assigned to the variable. I would surmise the intention of this in the specification was that it would be useful for control structure flexibility, but the truth is if you're not awaiting a Promise outright, you will find that almost all of the time you may as well be using a normal variable and you're just spinning gears:


What does MDN Say?

Description of Await MDN

Exceptions:

Most of the time, as stated, await is only useful when utilized with a Promise. To be balanced, an example of a situation where a value could be useful to use with an await function is when providing either an API call or a reference to test data depending on the current Environment.

Otherwise it's a rarity for reasons already stated. Hope this helps!

zfrisch
  • 8,474
  • 1
  • 22
  • 34
0

You can await a non-async (non Promise returning) function:

function sum(num1, num2) { return num1 + num2 };
async function main() {
    const s = await sum(2, 3); // this resolves immediately
}

Yep, your second function runs slower:

async function someAsyncFunc() {
    const user = await asyncGetUser();
    const categories = await asyncGetCategories(); 
    const mapping = await asyncMapUserWithCategory(user, categories);
};

because asyncGetCategories() will only be called after asyncGetUser() has settled.


Whereas in your first example, these two calls run simultaneously:

async function someAsyncFunc() {
    const [user, categories] = await Promise.all(asyncGetUser(), asyncGetCategories()];
    const mapping = await asyncMapUserWithCategory(user, categories);
};

This would run similarly to:

async function someAsyncFunc() {
    const userPromise = asyncGetUser();
    const categoriesPromise = asyncGetCategories();
    const user = await userPromise;
    const categories = await categoriesPromise;
    const mapping = await asyncMapUserWithCategory(user, categories);
};

In both these examples, the two async operations are started simultaneously, without one waiting on the other to settle.

Arash Motamedi
  • 9,284
  • 5
  • 34
  • 43
0

First, respect to the title of your question, all functions marked with async returns a Promise, even if you don't return explicitly one.

In case I did the same without Promise.all (below, I suppose slower) would the await-ed function also need to return promises for this to work?

Yes (logically, to work, don't). If you use await, it means that the function await-ed returns a Promise; otherwise, there is not logic reason to use await in synchronous functions.

I suppose slower

Well, there is no major difference using Promise.all than running the promises each per time. In fact, Promise.all does not execute the promise because the task of the promise is started when you instantiate it.

gugadev
  • 203
  • 1
  • 8