Please correct me if I am wrong.
As per my understanding .then() will only execute once the dependent function finishes its task. However here I could see different order of execution.
Suppose array size is 20, console.log(i) (inside axios) prints in different order like 18, 12, 11,3,15 ... so on. using .then() can't we make it synchronous?.
I know adding await keyword in front of Axios would work perfectly. But, just to make sure, is there any other way to achieve it?.
Observed Behaviour.
- When we execute this without await, this would work for few array items (it will be very fast).
- When we execute using await keyword it will take too much time.
var pdflist = ['one','two','three','four'.........'twenty']
for (let i=0; i<pdflist.length;i++)
{
let uri = {
method: 'GET',
url:'https://pdffiles/'+pdflist[i]+'.pdf',
responseType: "arraybuffer"
};
axios(uri).then(results => {
console.log(i) // prints in different order
console.log(results)
})
}