Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] return a promise when there is no callback in concat #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ get.concat('http://example.com', function (err, res, data) {
})
```

or with async/await

```js
const get = require('simple-get')

async function run () {
const { res, data } = await get.concat('http://example.com')
console.log(res.statusCode) // 200
console.log(data) // Buffer('this is the server response')
})
}

run().then(({ data } => console.log(data.toString()))
```

### POST, PUT, PATCH, HEAD, DELETE support

For `POST`, call `get.post` or use option `{ method: 'POST' }`.
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ function simpleGet (opts, cb) {
}

simpleGet.concat = (opts, cb) => {
if (cb) return _concat(opts, cb)
return new Promise((resolve, reject) => {
_concat(opts, (err, res, data) => err ? reject(err) : resolve({ res, data }))
})
}

function _concat (opts, cb) {
return simpleGet(opts, (err, res) => {
if (err) return cb(err)
concat(res, (err, data) => {
Expand Down
21 changes: 21 additions & 0 deletions test/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,24 @@ test('get.concat json error', function (t) {
})
})
})

test('get.concat with Promise', function (t) {
t.plan(3)
var server = http.createServer(function (req, res) {
res.statusCode = 200
res.end('blah blah blah')
})

server.listen(0, function () {
var port = server.address().port
get
.concat('http://localhost:' + port)
.then(({ res, data }) => {
t.equal(res.statusCode, 200)
t.ok(Buffer.isBuffer(data), '`data` is type buffer')
t.equal(data.toString(), 'blah blah blah')
server.close()
})
.catch(t.fail)
})
})