jamwt
jamwt•3w ago

no need to use promise.all(). and I'd

no need to use promise.all(). and I'd keep your batches between 100-1000
7 Replies
Mark L
Mark L•3w ago
So instead of this:
await Promise.all(
data.map(async (row) => {
try {
await ctx.db.insert('table', row);
} catch (error) {
...
}
})
);
await Promise.all(
data.map(async (row) => {
try {
await ctx.db.insert('table', row);
} catch (error) {
...
}
})
);
I should just do:
data.map(async (row) => {
try {
await ctx.db.insert('table', row);
} catch (error) {
...
}
})
data.map(async (row) => {
try {
await ctx.db.insert('table', row);
} catch (error) {
...
}
})
? or a for of loop?
jamwt
jamwtOP•3w ago
yeah, that works! the reason is the inserts aren't actually doing I/O. they're just queuing up writes that will all be flushed if/when the mutation succeeds I'd prefer just a simple for loop, yeah
Mark L
Mark L•3w ago
Thanks @Jamie ! It's quite confusing when you get started with Convex, that you are writing code that seems like it would be extremely inefficient, but gets translated into something that runs fast 😅
jamwt
jamwtOP•3w ago
yeah. I mean, this detail is a little subtle. but if you're querying data (db.query(...)), making things concurrent with promise.all does help, b/c that might actually do network I/O if values aren't in the cache but for writes (insert/update etc) there is no need to do this, since they're not actually async under the covers
Mark L
Mark L•3w ago
good to know! Thanks!
erquhart
erquhart•3w ago
Does this mean insert/update don’t actually need to be awaited in general?
jamwt
jamwtOP•3w ago
I would still await them to be safe. Otherwise, our protection about un awaited promises might kick in.

Did you find this page helpful?