3 回答

TA貢獻1848條經驗 獲得超6個贊
我最終在這里試圖找到類似問題的解決方案,所以對于像我這樣的其他不幸的小伙子,將粘貼我發現的內容,以下工作:
async function A(B: () => Promise<void>) {
await B();
}
現在我想調用A并傳遞一個異步函數,然后我這樣做:
await A(async () => {
await wait(3000);
})

TA貢獻1812條經驗 獲得超5個贊
異步函數只不過是一個函數返回承諾。取樣。
const getPromise = () => Promise.resolve("1")
const store = (fn) => {
fn().then(console.log)
}
store(getPromise)
const storeCB = (fn, cb) => {
fn().then(cb)
}
store(getPromise, console.log)
const storeThen = (fn) => {
return fn().then(x => "append: " + x)
}
storeThen(getPromise).then(console.log)
const getAsync = async () => "2"
store(getAsync)
const storeWithAwait = async (fn) => {
const restult = await fn()
return restult
}
storeWithAwait(getAsync).then(console.log)
添加回答
舉報