Await server onUploadComplete hook on the client.

Is there a way or a recommended pattern to run some side effect in the onUploadComplete hook in UT router and only once it's done resolve startUpload promise on the client? In my app a user can leave a review with an image attached - first client sends a request with review data and it gets saved to the database, once that resolves client starts an image upload and in the onUploadComplete hook on the server I update the db review row with a newly created file key. Problem is - I would like to refetch review data on the client to sync it with the server once data is successfully updated. But there doesn't seem to be a mechanism to await until onUploadComplete resolves and so what I got is a race condition if a client's sync request will be after(good) or before(bad) hook updates my db with new file key. Code more or less looks like this.
// client
await postReview(review);
await uploadImage(image);
sync(); // <- I would like this to run only after updateReview on the server finishes

// server - UT router
.middleware(...)
.onUploadComplete(async ({ file, metadata: { data } }) => {
await updateReview(data, file);
}),
// client
await postReview(review);
await uploadImage(image);
sync(); // <- I would like this to run only after updateReview on the server finishes

// server - UT router
.middleware(...)
.onUploadComplete(async ({ file, metadata: { data } }) => {
await updateReview(data, file);
}),
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server