我有两个函数a
,b
它们是异步的,前者没有await
,后者则是await
.他们都记录了一些东西到控制台并返回undefined
.在调用任一函数后,我记录另一条消息,并查看是否在执行函数体之前或之后写入消息.
function someMath() {
for (let i = 0; i < 3000000; i++) { Math.sqrt(i**5) }
}
function timeout(n) {
return new Promise(cb => setTimeout(cb, n))
}
// ------------------------------------------------- a (no await)
async function a() {
someMath()
console.log('in a (no await)')
}
// ---------------------------------------------------- b (await)
async function b() {
await timeout(100)
console.log('in b (await)')
}
clear.onclick = console.clear
aButton.onclick = function() {
a()
console.log('after a (no await) call')
} …
Run Code Online (Sandbox Code Playgroud)有时我的一些事件没有记录在 Firebase DebugView 中,我目前正试图找出原因。
我们决定省略await
for firebase.analytics().logEvent(...)
,现在我想知道这是否会导致丢失的事件。根据我的理解,这不应该对行为产生影响,因为我不必等待事件被记录。
所以我的问题是:在以下两种情况下,它是否会对事件日志的可靠性产生影响?
// With await
await firebase.analytics().logEvent('event_name');
// Without await
firebase.analytics().logEvent('event_name');
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个async
调用另一个async
函数的函数:
const func1 = async () => {...}
const func2 = async () => {func1()}
Run Code Online (Sandbox Code Playgroud)
我想知道使用内部await
调用是否仍然有意义或根本没有必要?func1()
func2
const func2 = async () => {await func1()}
Run Code Online (Sandbox Code Playgroud)