Understanding asynchronous javascript

What is the difference between the following code:

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);
}

      

+3


source to share


1 answer


The difference between your two functions: await

vs return

. Absence is await

not a big deal, since a return from a promise async function

will always be resolved with the samereturn await

promise result . So the only difference is implicitly returned undefined

in the first solution.

Explicitly written:

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;
}

      

+4


source







All Articles