From e394f7a3a1d6f494d447d43cd778b808d32d8df7 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 4 Oct 2018 11:42:28 +0200 Subject: [PATCH] do not use try-catch in the example --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1ca213b..e0c7705 100644 --- a/README.md +++ b/README.md @@ -106,13 +106,14 @@ fastify.register(require('fastify-postgres'), { }) fastify.post('/user/:username', (req, reply) => { - fastify.pg.transact(async client => { - try { - const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username]) - reply.send(id) - } catch (err) { - reply.send(err) - } + // will return a promise, fastify will send the result automatically + return fastify.pg.transact(async client => { + // will resolve to an id, or reject with an error + const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username]) + + // potentially do something with id + + return id }) })