Skip to content

Commit 2994680

Browse files
mcollinacemremengu
authored andcommitted
do not use try-catch in the example (#27)
1 parent 4f43a1d commit 2994680

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ fastify.register(require('fastify-postgres'), {
106106
})
107107

108108
fastify.post('/user/:username', (req, reply) => {
109-
fastify.pg.transact(async client => {
110-
try {
111-
const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
112-
reply.send(id)
113-
} catch (err) {
114-
reply.send(err)
115-
}
109+
// will return a promise, fastify will send the result automatically
110+
return fastify.pg.transact(async client => {
111+
// will resolve to an id, or reject with an error
112+
const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
113+
114+
// potentially do something with id
115+
116+
return id
116117
})
117118
})
118119

0 commit comments

Comments
 (0)