なので async 関数が promise 返しても then で受け取れるのは promise じゃなくて promise の中の値
async function foo() {
const value = await something()
const promise = longProcess(value.bar)
return promise
}
async function main() {
const promise = await foo()
promise.then(() => {
console.log("LONG PROCESS END")
})
console.log("LONG PROCESS START")
}
main()
await foo() の結果の promise は longProcess が返す promise の中の値なので then はなくてエラー
オブジェクトに包めば展開されないので promise を返せる
async function foo() {
const value = await something()
const promise = longProcess(value.bar)
return { promise }
}
async function main() {
const { promise } = await foo()
promise.then(() => {
console.log("LONG PROCESS END")
})
console.log("LONG PROCESS START")
}
main()
⇩実行用に longProcess とかのサンプル実装
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
const something = async () => {
await wait(1000)
return { foo: 1, bar: 2 }
}
const longProcess = async () => {
await wait(5000)
}