Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bendrucker/stream-to-promise
Convert streams (readable or writable) to promises
https://github.com/bendrucker/stream-to-promise
Last synced: 22 days ago
JSON representation
Convert streams (readable or writable) to promises
- Host: GitHub
- URL: https://github.com/bendrucker/stream-to-promise
- Owner: bendrucker
- License: mit
- Created: 2014-03-16T20:13:09.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-02-14T04:03:55.000Z (9 months ago)
- Last Synced: 2024-05-15T13:09:17.210Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 43 KB
- Stars: 137
- Watchers: 5
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs - stream-to-promise - Convert streams (readable or writable) to promises. (Repository / Streams)
- awesome-nodejs-streams - bendrucker/stream-to-promise - Convert streams (readable or writable) to promises (Modules)
README
# stream-to-promise [![Build Status](https://travis-ci.com/bendrucker/stream-to-promise.svg?branch=master)](https://travis-ci.com/bendrucker/stream-to-promise)
> Convert streams (readable or writable) to promises
## Installing
```sh
npm install --save stream-to-promise
```## Examples
### Readable Streams
```js
streamToPromise(readableStream).then(function (buffer) {
// buffer.length === 3
})
readableStream.emit('data', new Buffer())
readableStream.emit('data', new Buffer())
readableStream.emit('data', new Buffer())
readableStream.emit('end') // promise is resolved here
```### Writable Streams
```js
streamToPromise(writableStream).then(function () {
// resolves undefined
})
writableStream.write('data')
writableStream.end() // promise is resolved here
```### Error Handling
```js
const err = new Error()
streamToPromise(stream).catch(function (error) {
// error === err
})
stream.emit('error', err) // promise is rejected here
```