1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nuxt3(Nitro/h3) server apiで時間のかかる処理をクライアント側に待ってもらう書き方

Posted at

Nitro/h3のserver apiでは、何もreturnしないと何故か404エラーになってしまう。
非同期処理などで、クライアント側にサーバー側の処理を待ってもらう書き方はこちら。

export default defineEventHandler(async (event) => {
  
  return await new Promise((resolve, reject) => {
      try {
        時間のかかる処理()
        resolve({success:true})
      } catch(e) {
        reject({success:false})
      }
  })
  
})

Promise()で包んであげた処理をreturnすれば、非同期な処理が終わってresolve()するまで待ってくれる。Promiseの中でreturnしてもだめ。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?