I would suggest using the first snippet. It does have an advantage - you can wait for the onAdd to finish if you wish. With the second example, you can execute it and the code would work - an insertion would be made, however you cannot wait for the completion.
Here is a simple example:
//simple delayed operation - prints a message after 2 seconds async function delayedOperation(msg) { return new Promise(resolve => { setTimeout(() => { console.log("complete:", msg); resolve(); }, 2000); }); } //awaiting the operation async function withAwait() { await delayedOperation("with await"); } //not awaiting the operation function withoutAwait() { delayedOperation("without await"); } async function main() { console.log("before calling withAwait()"); await withAwait(); console.log("after calling withAwait()"); console.log("before calling withoutAwait()"); await withoutAwait(); console.log("after calling withoutAwait()"); } main();
As you can see, the withoutAwait cannot itself be awaited. The problem is that it doesn't produce a promise, main() completes before the delayed operation in withoutAwait() finishes.
You can always have the await in the function but not use it, if it's not needed:
async function delayedOperation(msg) { return new Promise(resolve => { setTimeout(() => { console.log("complete:", msg); resolve(); }, 2000); }); } async function withAwait() { await delayedOperation("with await"); } async function main() { console.log("before calling withAwait()"); //not awaiting because we don't care whether it finishes before proceeding withAwait(); console.log("after calling withAwait()"); } main();
However, it's a good idea to check if the operation is completed, otherwise you cannot respond to errors or may not even know they happened:
//WILL FAIL! async function delayedOperation(msg) { return new Promise((resolve, reject) => { setTimeout(() => { reject(`problem when doing [${msg}]`); }, 2000); }); } async function withAwait() { await delayedOperation("with await"); } async function main() { console.log("before calling withAwait() and handling the error"); try { await withAwait(); } catch(e) { console.error(e); } console.log("after calling withAwait() and handling the error"); console.log("before calling withAwait() - no handling"); //not awaiting because we don't care whether it finishes before proceeding withAwait(); console.log("after calling withAwait() - no handling"); } main();
And a note, adding async but not await will not allow you to correctly await the result of the function.
async function delayedOperation(msg) { return new Promise(resolve => { setTimeout(() => { console.log("complete:", msg); resolve(); }, 2000); }); } // async function but not awaiting the operation async function withoutAwait() { delayedOperation("without await"); } async function main() { console.log("before calling withoutAwait()"); //attempting to await the completion await withoutAwait(); console.log("after calling withoutAwait()"); } main();
Since the body of the withoutAwait() doesn't have an await then it completes as soon as delayedOperation() returns a promise, rather than when that promise settles.
onAddto finish?db.collection('FlashCards').add(...)return a promise? Do you want the code followingonAdd()to "wait" for the operation to be completely done?