I have a loop with two await async functions. I would expect that the second function (F2) would fire after completion of the first await function (F1). But the loop continues without even starting F2. F2 fires only after the F1 async function fires.
I have provided tacking functions to follow the flow.
for (let i=0; i<5; i++){
const S3result = (async () => {
try {
console.log ('starting await promise 1' );
params.Key = '012/' + 'bob1' + i.toString();
await promiseS3(s3, params);
console.log ('complete 1st upload');
params.Key = '012/' + 'bob2' + i.toString();
console.log ('starting await promise 2' );
await promiseS3(s3, params);
console.log('completed 2nd upload');
} catch (e) {
console.log (e)
}
})()
const promiseS3 = (s3object,params) => {
return new Promise((resolve, reject) => {
console.log ('start s3 Object promise function');
s3object.upload(params, function(s3Err, data) {
if (s3Err) reject ('bad S3 call')
console.log(`File uploaded successfully at ${data.Location}`)
resolve (data);
});
})
}
I expected F1 to fire, await and then have F2 fire after F1 completes.
This is the output:
starting await promise 1 start s3 Object promise function starting await promise 1 start s3 Object promise function starting await promise 1 start s3 Object promise function starting await promise 1 start s3 Object promise function starting await promise 1 start s3 Object promise function after function File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob14 complete 1st upload starting await promise 2 start s3 Object promise function File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob11 complete 1st upload starting await promise 2 start s3 Object promise function File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob10 complete 1st upload starting await promise 2 start s3 Object promise function File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob12 complete 1st upload starting await promise 2 start s3 Object promise function File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob13 complete 1st upload starting await promise 2 start s3 Object promise function File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob22 completed 2nd upload File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob21 completed 2nd upload File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob23 completed 2nd upload File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob24 completed 2nd upload File uploaded successfully at https://filesafe-beta.s3.us-west-1.wasabisys.com/012/bob20 completed 2nd upload