javascript - 我应该在这个例子中使用异步等待吗
问题描述
似乎以下示例适用于异步等待,如下所示:
import { db } from '../firebase';
const onAdd = async () => {
await db.collection('FlashCards').add({
originalText: newOriginalText,
translatedText: newTranslatedText,
});
};
并且没有像这样的异步等待:
import { db } from '../firebase';
const onAdd = () => {
db.collection('FlashCards').add({
originalText: newOriginalText,
translatedText: newTranslatedText,
});
};
我使用哪一个有什么区别吗?在这个例子中使用 async await 有什么改善吗?
解决方案
我建议使用第一个片段。它确实有一个优势 - 如果您愿意,您可以等待onAdd
完成。对于第二个示例,您可以执行它并且代码将起作用- 将进行插入,但是您不能等待完成。
这是一个简单的例子:
//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();
如您所见,withoutAwait
不能等待本身。问题是它不会产生承诺,main()
在延迟操作完成之前withoutAwait()
完成。
如果不需要,您始终可以在函数中使用但不使用它:await
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();
但是,检查操作是否完成是个好主意,否则您将无法响应错误,甚至可能不知道它们发生了:
//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();
并注意,添加async
但不 await
添加不会让您正确地await
获得函数的结果。
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();
由于 the 的主体withoutAwait()
没有await
then 它在delayedOperation()
返回一个承诺时立即完成,而不是在该承诺完成时完成。
推荐阅读
- mysql - 如何在 GCP 上使用 MySQL 运行 Ghost?
- c# - 从 Jquery 将值传递给 href
- python-3.x - 在 tcolorbox 中写入 SQL 查询的输出时不需要的字符
- r - 如何根据一个变量(R)通过ggplot绘制线和点?
- c - 使用 sprintf()/write() 的 C 程序有多余的字符
- xamarin.forms - Xamarin 表单:当新版本可用时,AppStore 应用程序页面未打开?
- r - Twitter 流与 R 包 rtweet 过早断开连接
- winapi - SetWindowsHookEx() 设置的钩子按什么顺序接收消息?
- c# - .NetStandard 2.0 - 间接引用其他项目?
- amazon-web-services - eks create cluster 命令失败并出现错误:在输入字节 11 处计算指纹非法 base64 数据