Why can’t you just put Promise.all() everywhere — especially while accessing databases (Parallelization Part 2)

Aashish Peepra
9 min readApr 7, 2024

If you ever have been in a relationship, I’m sure you would be in a situation where you made just too many promises and now it’s getting hard to keep them. The problem is, that it seems that all of them can be executed independently and are almost mutually exclusive tasks. But you didn’t consider how these tasks might get affected by each other due to shared resources. You will need to solve the relationship problem yourself but I can help you understand the computer version of this.

P.S.: This one is a tough read. So if you just woke up and this is your first email, I suggest you grab the coffee first.

Let’s look at some code first

 async syncInvoiceIndexForAllRootFiCompanies(): Promise<boolean> {
const rootFiCompanies = await this.prisma.rootFiCompany.findMany({
where: {
corporationId: {
gt: 0,
},
},
include: {
corporation: true,
},
});

const invoiceSyncPromises = (
await Promise.all(rootFiCompanies.map(async rootFiCompany => await this.setInvoicesToDatabaseFuture(rootFiCompany.id, rootFiCompany.corporation.companyId)))
).flat();

try {
await this.prisma.$transaction(invoiceSyncPromises)…

--

--

Aashish Peepra

Software Engineer @Commenda | 100K+ people used my websites | Mentored 28K+ Developers globally | Google Solution challenge global finalist