The same as above, but using ES7 async/await syntax:
await db.task(async t => { const questions = await t.any('SELECT * FROM questions'); for(const q of questions) { q.votes = await t.any('SELECT id, value FROM votes WHERE question_id = $1', [q.id]); } return questions; }); // method "task" resolves with the correct data tree UPDATE-1
The following related answer offers more options, by concatenating child queries, which will give a much improved performance: Combine nested loop queries to parent result pg-promise.
UPDATE-2
Another example added, using ES7 async/await approach.