Cannot get data out of Promis.allSettled()
I am trying to get data from db which requires a an array of individual queries like this:
why is this happening?
const dbColumns = await ctx.db
.select()
.from(columns)
.where(eq(columns.projectId, projectId));
const promises = dbColumns.map(async (column) => {
const dbItems = await ctx.db
.select()
.from(items)
.where(eq(items.columnId, column.id));
return {
...column,
items: dbItems,
};
});
const columnsWithItems: ColumnWithItems[] = [];
Promise.allSettled(promises)
.then((results) => {
results.map((result) => {
if (result.status === "fulfilled") {
const columnWithItems = result.value;
columnsWithItems.push(columnWithItems);
console.log(columnsWithItems);
}
});
})
.catch((err) => console.error(err));
console.log(columnsWithItems); // empty array
const dbColumns = await ctx.db
.select()
.from(columns)
.where(eq(columns.projectId, projectId));
const promises = dbColumns.map(async (column) => {
const dbItems = await ctx.db
.select()
.from(items)
.where(eq(items.columnId, column.id));
return {
...column,
items: dbItems,
};
});
const columnsWithItems: ColumnWithItems[] = [];
Promise.allSettled(promises)
.then((results) => {
results.map((result) => {
if (result.status === "fulfilled") {
const columnWithItems = result.value;
columnsWithItems.push(columnWithItems);
console.log(columnsWithItems);
}
});
})
.catch((err) => console.error(err));
console.log(columnsWithItems); // empty array
0 Replies