I'm building a chunked file uploader. I understood that promises are the best way to handle the results but still, can't wrap my mind of how I should capture the promises from the outside.
caller.js
let fileUploader = new ChunkedUpload()
fileUploader.setFile(file)
fileUploader.upload().then(result => {
// Here I'd like to catch the current result of the file
// If it's not uploaded, I need the percentage and continue
// If it is I need the response from the API
console.log(result)
})
uploader.js
upload() {
let totalChunks = Math.ceil(this._file.size/this._sliceSize)
return this._chunk(0, 0, totalChunks)
}
_chunk(start, chunk, totalChunks) {
let lastChunk = false
let end = start + this._sliceSize
if (this._file.size - end < 0) {
end = this._file.size
lastChunk = true
}
let slice = this._sliceFile(this._file, start, end)
let formData = new FormData()
formData.append('chunk', chunk)
formData.append('chunks', totalChunks)
formData.append('file', slice)
return new Promise((resolve, reject) => {
axios.post(this._url, formData).then(response => {
if (lastChunk) {
// This part is okay, it returns the result
resolve({
uploaded: true,
url: response.data,
uploadPercentage: 100,
uploadSize: this.formatBytes(file.size)
});
} else {
// Here's the issue. Next chunk upload is called,
// however I cannot track the process in caller.js
resolve(this._chunk(
start += this._sliceSize,
chunk += 1,
totalChunks
));
}
})
})
}
I accept any comments and whatsoever. Maybe my approach is wrong, please let me know!