3

What is the difference between following piece of codes:

Code 1:

export default async function syncData() {
  await Promise.all([syncData1(), syncData2()]);
}

// called in sagas.js
function* x() {
  const res = yield call(syncData);
}

Code 2:

export default function syncData() {
  return Promise.all([syncData1(), syncData2()]);
}
// called in sagas.js
function* x() {
  const res = yield call(syncData);
}
john doe
  • 806
  • 3
  • 14
  • 28
  • I believe the only difference is that the former returns an asynchronous function object, whereas the latter doesn't. – Chris May 23 '17 at 11:31
  • What is `call`, and why are you `yield`ing its result? – Bergi May 23 '17 at 11:42
  • No discernible difference, both are syntax errors. You cannot have `yield` in a top level module scope. – Bergi May 23 '17 at 11:44

1 Answers1

4

The difference between your two functions is await vs return. The missing await doesn't really matter, since returning a promise from an async function will always resolve with the promise result similar to return await. So the only difference is the implicitly returned undefined in the first solution.

Written out explicitly:

export default async function syncData() {
  const res = await Promise.all([syncData1(), syncData2()]);
  return undefined;
}

export default function syncData() {
  const res = await Promise.all([syncData1(), syncData2()]);
  return res;
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375