I've a Upload file functionality, I send the file in parts so, many request was made, when I upload the file I use async : 'true'
in $.ajax({});
, then when I click in other tab, request to get list of book's names for example ... and request is waiting and is made when all request of Upload was finished.
I need a promise in my code ? or $.when()
is better ?
My code is:
$("#Upload").click(function (){
...
sendFile(file, blockLength, numberOfBlocks).then(()=>{
//All request are made
});
});
async function sendFile(file, chunkSize, numberOfBlocks) {
...
function sendNextChunk () {
$.ajax({
async: true,
type: 'POST', url: "URL",
headers: headers,
data: fileChunk,
cache: false,
contentType: false,
processData: false
}
...
if (currentChunk <= numberOfBlocks) {
await sendNextChunk();
}
}
await sendNextChunk();
}
In the network is like:
/UPload?chunk=1 200
/UPload?chunk=2 200
/UPload?chunk=3 200
/getBooksList pending < - Here is pending until all request of Upload are made
/UPload?chunk=4 200
/UPload?chunk=5 200
/UPload?chunk=6 pending
/UPload?chunk=7 pending
/UPload?chunk=8 pending
/UPload?chunk=9 pending
/UPload?chunk=10 pending
...