In the onClientUploadComplete
of my file upload, I populate an input field value (via React-hook-form):
const { : startAvatarUpload } = ("avatar", {
onClientUploadComplete(res) {
if (!res.length) throw new Error("Avatar uploadResult empty");
form.setValue("avatarUrl", res[0].url);
},
});
When I upload the image, I store the promise in state:
<AvatarInput
onFileSelected={(file) =>
setRunningAvatarUpload(startAvatarUpload([file]))
}
/>
The goal is to avoid submitting the form before the file upload has finished. So I await the stored promise in the onSubmit
function:
async function onSubmit() {
try {
if (runningAvatarUpload) await runningAvatarUpload;
// The promise has resolved so the URL form field should be set
const values = form.getValues();
await updateUserProfile(values);
onOpenChange(false);
} catch (error) {
...
}
}
My question is: Is it guaranteed that onClientUploadComplete
will have finished before the runningAvatarUpload
resolves? Otherwise I might submit the form before the URL field has been populated.