【发布时间】:2022-01-01 08:25:39
【问题描述】:
问题
异步函数中的异步函数不等待?
代码
// onConfirmed executes after confirming on modal
onConfirmed={async () => {
const res = await submit(submitData) // post data and return some data
if (!res.error) {
const resFb= await Fb(data)
console.log(resFb) // ***execute it before waiting "await Fb(data)"
} else {
// handle error
}
}}
//Fb function
export const Fb = async (data) => {
const body = {
method: 'share',
href: 'https://hogehoge',
display: 'popup',
hashtag: '#hogehoge',
quote: `content: ${data.content}`,
}
return FB.ui(body, async (res) => {
if (res !== undefined && !res.error_code) {
return await Api.put(body) // put to own server (executes here without problem)
} else {
return res
}
})
}
我需要获取等待异步函数的 resFb 的正确值。
【问题讨论】:
-
Fb需要等待FB.ui -
如果
FB.ui()不是async,则需要转换Fb()以使用显式承诺。
标签: javascript asynchronous async-await facebook-sdk-4.0