1

Are these two snippets the same?

The top version returns a promise in the arrow function, and the bottom one doesn't return anything. Is there an implicit return when using async, should I return null, do I need to return the const info?

When i look at how babel translates them it replaces async/await with generators that make it look like i should return the const info

// t.get() is a promise, set and delete are not

db.runTransaction(t => {
  return t.get(infoRef).then(info => {
    t.set(db.doc(`/users/${uid}`), info.data());
    t.delete(infoRef);
  });
});

db.runTransaction(async t => {
  const info = await info.get(infoRef)
  t.set(db.doc(`/users/${uid}`), info.data());
  t.delete(infoRef);
});

babel link

AKnox
  • 2,455
  • 3
  • 19
  • 19

2 Answers2

1

There is no implicit return, but any returned value is wrapped in a promise.

AKnox
  • 2,455
  • 3
  • 19
  • 19
0

Async functions implicitly return a promise.

Returning a value from one promise passes that value as an argument to the next if the promise is chained.

If you don't actually chain anything after the first promise, then the return value is ignored, and there is no point to including it.

patstuart
  • 1,931
  • 1
  • 19
  • 29