wait until onload finish JavaScript
const processFile = (file) => {
const reader = new FileReader()
return new Promise((resolve, reject) => {
reader.onload = (fileEvent) => {
const fileContents = fileEvent.target.result
const processedContents = performPreprocessingOnFile(fileContents)
resolve(processedContents)
}
reader.onerror = () => {
reject('oops, something went wrong with the file reader.')
}
reader.readAsArrayBuffer(file)
})
}
processFile(myFile).then((processedContents) => {
fetch('https://my-backend-site/post-endopint', {
method: 'POST',
body: processedContents,
})
})