Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ The simplest possible way to connect, query, and disconnect is with async/await:

```js
import { Client } from 'pg'
const client = new Client()
await client.connect()
const client = await new Client().connect();

const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
Expand Down Expand Up @@ -83,5 +82,3 @@ console.log(res.rows[0].message) // Hello world!
```

Our real-world apps are almost always more complicated than that, and I urge you to read on!


2 changes: 1 addition & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class Client extends EventEmitter {
if (error) {
reject(error)
} else {
resolve()
resolve(this)
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Client.prototype.connect = function (callback) {
if (error) {
reject(error)
} else {
resolve()
resolve(this)
}
})
})
Expand Down
8 changes: 8 additions & 0 deletions packages/pg/test/integration/client/promise-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ suite.test('valid connection completes promise', () => {
})
})

suite.test('valid connection returns the client in a promise', () => {
const client = new pg.Client()
return client.connect().then((clientInside) => {
assert.equal(client, clientInside)
return client.end().then(() => {})
})
})

suite.test('invalid connection rejects promise', (done) => {
const client = new pg.Client({ host: 'alksdjflaskdfj', port: 1234 })
return client.connect().catch((e) => {
Expand Down