[英]How to call async/await function asynchronously in other functions?
[英]Call a function only after other async/await functions are resolved
我在單擊事件后調用 2 個函數。
這些 function 調用存在於singInHandler()
signInHandler = async () => {
await window.customAuth.init(configFile);
await window.customAuth.login();
}
要求:我想調用第 3 個 function window.customAuth.getId()
但只有在執行上述 2 個函數和相應的響應后才能調用它。
window.customAuth.init(configFile)
@returns {Promise} 帶有用戶數據的 promise。
window.customAuth.login()
@returns {Promise}
我怎樣才能做到這一點?
將您的承諾放入數組中,然后調用Promise.race
,它將在 1 promise 得到解決后立即得到解決。
或更短:
Promise.race([window.customAuth.init(configFile), window.customAuth.login()]).then(() => {
// logic here
})
你可以重復你已經做過的事情。
signInHandler = async () => {
await window.customAuth.init(configFile);
await window.customAuth.login();
await window.customAuth.getId();
}
如果你想檢查前 2 個函數的返回值,那么……就去做吧。
signInHandler = async () => {
const result1 = await window.customAuth.init(configFile);
const result2 = await window.customAuth.login();
if (result1 === true || check whatever you want) {
await window.customAuth.getId();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.