这个异步函数有什么作用?
我是JS中异步/等待异步的新手。这是我正在查看的离子React应用程序,我不确定它的作用。有人可以像我5岁那样向我解释吗?
const handleClick = async(e, operator) => {
if(operator == "MC"){
if(isPlatform("android")){
await Storage.remove({key:'history'})
}else{
window.localStorage.removeItem("history")
}
return false
}else if(operator == "MR"){
let storage = ""
let parse = ""
if(isPlatform("android")){
let storage = await Storage.get({key:'history'})
parse = JSON.parse(storage.value)
}else{
let storage = await window.localStorage.getItem("history")
parse = JSON.parse(storage)
}
setSumHistory(parse[0].expration)
return false
}}
I'm new to async/await in JS. This is an ionic react app I'm looking at and I'm not sure what it does. Can someone explain it to me like I'm 5?
const handleClick = async(e, operator) => {
if(operator == "MC"){
if(isPlatform("android")){
await Storage.remove({key:'history'})
}else{
window.localStorage.removeItem("history")
}
return false
}else if(operator == "MR"){
let storage = ""
let parse = ""
if(isPlatform("android")){
let storage = await Storage.get({key:'history'})
parse = JSON.parse(storage.value)
}else{
let storage = await window.localStorage.getItem("history")
parse = JSON.parse(storage)
}
setSumHistory(parse[0].expration)
return false
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您要求澄清异步/等待,因此我将介绍该部分。
如果将异步添加到函数中,则意味着该函数将始终返回承诺。
https://javascript.info/ashync-await 等待以下几行希望解决或拒绝。
如果这些呼叫中的一个被拒绝带有错误的Handleclick,因为在这些等待的呼叫周围没有尝试 /捕获块。
https://javascript.info/try-catch
希望这将帮助您更好地了解async async async在此代码块中等待。
Since you ask for clarification for async/await I will cover that part.
If you add async to a function this means that the function will always return a promise. https://javascript.info/async-await
The
handleClick
function will wait on the following lines for a promise to resolve or reject.If one of these calls gets rejected handleClick with throw an error since there is no try / catch block around these await calls.
https://javascript.info/try-catch
Hope this will help you get a better understanding of async await in this code block.
如果有一条线需要时间来解决,而下一行取决于该行的结果,则您需要等待通过返回 Promise Promise 。
要告诉该功能,您需要解决一行,这将返回承诺,您需要将 async 添加到该功能。
if there is a line that needs time to be resolved and the next line depends on on the results of that line of code, you need to await that line to complete by returning a promise.
and to tell the function there is a line you need to resolve that will return a promise you need to add async to that function.