英文:
Not using "await" on network calls
问题
我工作的应用程序几乎在每次按钮点击时都会触发分析事件。尽管这些事件会触发与我们的分析服务器的网络调用,但我们并没有使用await
,因为
- 我们并不特别关心这个调用是否失败,以及
- 当我们避免在每次按钮点击时等待网络调用时,可以使我们的用户界面更加流畅。
避免这种策略是否存在任何潜在问题?
英文:
The application I work in fires analytics events on almost every button click. Although these events trigger a network call to our analytics server, we haven't been using await
because
- we don't really care if this call fails, and
- it makes our UI a bit snappier when we avoid waiting for a network call on every button click.
Are there any pitfalls with avoiding this strategy?
答案1
得分: 0
你在继续之前不需要等待这些调用,但如果你关心正确处理错误,应该附加一个.catch()
调用。添加一个.catch()
调用将允许你的应用逻辑继续执行,同时仍然允许你处理错误。如果在错误发生时没有任何操作(不重试,不报告,真的什么都不做),那么你可以让拒绝的情况发生。
每当你创建一个没有await
和没有.catch()
的Promise时,这相当于调用一个没有try-catch包装的函数抛出错误。如果这对你来说没问题,那就可以。
个人建议至少添加一个.catch(console.error)
,甚至是.catch(() => {})
作为最低要求。请注意,.catch(() => {})
会悄悄地丢弃任何拒绝,所以只有在你真的不关心结果时才使用它。
英文:
You don't need to await
these calls before proceeding, but you should attach a .catch()
call if you care about handling errors properly. Adding a .catch()
call will allow your application logic to proceed uninterrupted while still allowing you to handle the errors. If there is nothing to do on the error (no retry, no report, literally nothing) then you could just let the rejection go.
Every time you create a promise with no await
and no .catch()
, it's the equivalent of calling a function that throws an error with no try-catch wrapper. If that's okay with you, then it's ok.
Personally, I recommend at least adding a .catch(console.error)
or even .catch(() => {})
at the very minimum. Be aware that .catch(() => {})
will silently discard any rejections, so only use that if you really really don't care about the result.
答案2
得分: 0
我会说这在很大程度上取决于您的期望/目标。如果呼叫在不必完全完成以使用您的应用程序的情况下,那么在这种情况下使用await可能是没有意义的。通常,当您必须等待响应完成后才能继续执行应用程序时,需要使用await/async。
然而,正如其他人提到的,您应该考虑您的错误策略。如果 Firebase 的调用之一失败了会怎样?您是否需要回滚某些内容,通知用户或执行其他操作?通常情况下,您应该始终拥有错误策略。因此,您不一定要使用await/async,但至少应该在调用之后使用.catch() 来处理错误状态。
英文:
I would say it mostly depends on your expectations/goals. If the call doesn't need to be fully completed to use your application, then using await could be useless for your case. In general, await/async is needed when you have to wait for the response to be completed before continuing the execution of the application.
However, as others mentioned, you should think about your error strategy. What if one of the Firebase call fails? Do you need to revert something, inform your user, or anything else? In general, you should always have an error strategy. So, you don't have to use await/async, but you should at least use .catch() after your call to manage an error state.
答案3
得分: 0
在不调用await
时没有任何问题。
首先,什么是await
?它只是一种语法糖,以同步的方式表示异步操作。经验法则是:仅在以下情况之一时使用await
才有意义:
- 你想在完成后立即执行某事
- 你需要将从其Promise返回的值赋给变量
否则,await
毫无意义。
英文:
There is nothing wrong with not calling await
.
First of all, what is await
? It's just a syntax sugar to express async operation in a synchronous way. Rule of thumb is: using await
only makes sense if either of these is true:
- You want to do something right after it's done
- You need to assign the value returned from its Promise
Otherwise it makes zero sense to await
it.
答案4
得分: 0
你应该查看 sendBeacon()
函数:
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
这个API是专门设计用于向分析发送数据的,正如你所建议的,它不会等待网络调用完成。此外,用户可以导航到其他页面,数据仍会在后台发送。
英文:
Assuming we are talking about a browser, you should check out sendBeacon()
function:
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
This API was designed specifically for sending data to analytics and as you suggested, it doesn't wait for the network call to complete. Moreover, the user can navigate away and the data will still be sent in the background.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论