Reputation: 322
I was writing an automation script for DB manipulation in Prisma. I'm using PostgreSQL as my database. I noticed that whenever I omit "await" keyword on database updations, it wouldn't show any error but the database changes are not reflected.
// Does not work
prisma.model.create({
data: {
name: 'tai',
schema: {
abc: 8,
def: 8,
},
}
})
But once I use "await", the database does get updated. I skimmed through the documentation but did not have any luck. Can anybody explain the purpose of this behaviour. Also how they managed to implement it.
// Works
await prisma.model.create({
data: {
name: 'tai',
schema: {
abc: 8,
def: 8,
},
}
})
Upvotes: 9
Views: 5373
Reputation: 7238
This happens because Prisma queries are then-ables, meaning they only execute when you call await
or .then()
or .catch()
. This is called lazy evaluation. This is different than a regular promise which starts executing immediately. There's an issue in the Prisma docs repository about this which you can check out for more information.
How to implement: Check out this library: https://github.com/sindresorhus/p-lazy
Upvotes: 19