Promise.all not behaving as expected

await Promise.all([
db.update(users).set({ name }).where(eq(users.id, userId)),
postId && db.update(posts).set({ title: 'Title' }).where(eq(postId.id, postId)))
]);
await Promise.all([
db.update(users).set({ name }).where(eq(users.id, userId)),
postId && db.update(posts).set({ title: 'Title' }).where(eq(postId.id, postId)))
]);
In the case where postId is defined, i would expect the Promise.all to wait for the post update. Instead, it only works if i do await db.update. Shouldn't postId && db.update resolve to just the db.update value? if this is a bug i can create a GH issue
7 Replies
Angelelz
Angelelz11mo ago
This is javascript How can drizzle affect the behavior of promise.all?
jakeleventhal
jakeleventhalOP11mo ago
im not so sure about this:
const sleepThenLog = async (ms: number) => {
await new Promise((r) => {
setTimeout(r, ms);
});
console.log('done with', ms);
};

await Promise.all([
sleepThenLog(1000),
true && sleepThenLog(2000)
]);
console.log('done');
const sleepThenLog = async (ms: number) => {
await new Promise((r) => {
setTimeout(r, ms);
});
console.log('done with', ms);
};

await Promise.all([
sleepThenLog(1000),
true && sleepThenLog(2000)
]);
console.log('done');
this code will log "done" after both sleep statements complete
Angelelz
Angelelz11mo ago
Hmm, Drizzle implement lazy promises on all the query builders try this:
await Promise.all([
db.update(users).set({ name }).where(eq(users.id, userId)).execute(),
postId && db.update(posts).set({ title: 'Title' }).where(eq(postId.id, postId))).execute()
]);
await Promise.all([
db.update(users).set({ name }).where(eq(users.id, userId)).execute(),
postId && db.update(posts).set({ title: 'Title' }).where(eq(postId.id, postId))).execute()
]);
I'm not sure I'd write code like this though
jakeleventhal
jakeleventhalOP11mo ago
so for any promise.all, is it required to either do await or add .execute at the end?
Angelelz
Angelelz11mo ago
Don't do await, use the execute, this will actually convert it to a normal promise
jakeleventhal
jakeleventhalOP11mo ago
why is it better to not use await? and are there other use-cases for .execute()?
Angelelz
Angelelz11mo ago
because if you use await you defeat the purpose of promise.all You will await it You'll be basically passing an unwrapped promise, that has already been awaited The execute method will turn the query builder into a regular promise. Without it, the query builder behave like any other regular object, but if you await it, it becomes a promise that's now being resolved
Want results from more Discord servers?
Add your server