When you use await
in an async function, it stops the execution until the Promise is resolved. This makes each line wait for the previous, which yields a code that looks almost like a synchronous one:
await s3.copyObject({/*...*/}).promise();
await s3.deleteObject({/*...*/}).promise();
The above code copies an object, then it deletes one. In case you wanted to move the object, it is the desirable chain of events as you want the copy operation to finish before deleting the original.
But what if there is an unrelated call, such as an update to a database?
await s3.copyObject({/*...*/}).promise();
await s3.deleteObject({/*...*/}).promise();
await dynamodb.updateItem({/*...*/}).promise();
While the dynamodb.updateItem
has nothing to do with the S3 object, it still waits for the move operation (copy + delete) to finish. This makes the overall process longer than necessary.
In this case, a better implementation is to run the two parts in parallel. To make it easier to see, let's group the two operations with async functions:
const moveObject = async () => {
await s3.copyObject({/*...*/}).promise();
await s3.deleteObject({/*...*/}).promise();
};
const updateDatabase = async () => {
await dynamodb.updateItem({/*...*/}).promise();
};
JavaScript has a built-in convenience method to run things in parallel. This is the Promise.all
, that gets a collection of Promises and returns a Promise with all the results. Whenever you want to run multiple things concurrently, this is the tool to use.
To use it to run the two operations in parallel, use:
await Promise.all([moveObject(), updateDatabase()]);
There are multiple things to notice here. First, Promise.all
gets Promises. Remember that calling an async function returns a Promise, so don't forget to call the functions you want to run.