javascript - Await 不使用 firebase 批处理功能等待
问题描述
好吧,有一个简单的函数可以将加载程序的值设置为 true,并且在函数完成后将其设置为 false。但是有一个问题,我的 async/awaitasync/await
函数的 await 不等待具有firebase batch writes
. 我的代码:
const updateNegotiation = async title => {
negotiationLoader.value = true
await store.dispatch('actionUpdateNegotiation', {
negotiation: listFromDialog.value,
title: title,
loader: negotiationLoader.value
})
negotiationLoader.value = false // the code doesnt await and put th loader's value to false
}
这是我的firebase功能batch writes
//vuex
async actionUpdateNegotiation({commit}, deal){
const batch = db.batch()
const reference = db.collection('negotiations')
.doc(moduleUser.state.user.email)
.collection('deals')
const setRef = reference.doc(deal.title)
.collection('clients')
.doc(deal.negotiation.id)
batch.set(setRef, deal.negotiation)
batch.update(setRef, {
...deal.negotiation,
status: deal.title
})
const deleteRef = reference.doc(deal.negotiation.status)
.collection('clients')
.doc(deal.negotiation.id)
batch.delete(deleteRef)
try{
batch.commit()
return {
res: false
}
} catch (error) {
console.log(error);
return{
error,
res: true
}
}
}
解决方案
关键字仅在await
您调用的代码返回承诺时才起作用。您似乎所做的只是标记actionUpdateNegotiation
为async
,这还不够。
据我所知,内部唯一的异步操作actionUpdateNegotiation
是对 的调用batch.commit()
,因此您可以使函数返回一个承诺:
return batch.commit()
或者,您可以使用await
:
await batch.commit()
推荐阅读
- aeron - 在生产中的 Aeron 集群中设置文件同步级别 0 是否安全?
- android - 获取视图的位置并将其设置为 x,y 以用于另一个视图,但它们不匹配
- python - 我可以使用带有 dtype=object 的 numpy 数组在类实例之间共享任意类型的列表吗?
- python - pandas groupby 单列但添加多列的内容
- docker - yum 在 Centos 上安装 Docker | 需要容器-selinux libseccomp slirp4netns fuse-overlayfs 错误
- audio - Fedora 容器中的 Pipewire 音频
- postgresql - EDB Postgres Replication Server - 无法添加数据库连接。控制器数据库尚未初始化
- python - 在 3d matplotlib 图中扩展网格线
- c# - 即使在配置 ASP.NET Core Web API 中启用了 CORS,CORS 也会阻止发布请求
- linux - Proxychains4 - 如何将 proxychains4 与 .ovpn 文件一起使用