-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93a305e
commit dae1d7d
Showing
1 changed file
with
27 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
stream-to-promise [![Build Status](https://travis-ci.org/bendrucker/stream-to-promise.svg)](https://travis-ci.org/bendrucker/stream-to-promise) [![NPM version](https://badge.fury.io/js/stream-to-promise.svg)](http://badge.fury.io/js/stream-to-promise) | ||
stream-to-promise [![Build Status](https://travis-ci.org/bendrucker/stream-to-promise.svg)](https://travis-ci.org/bendrucker/stream-to-promise) | ||
================= | ||
|
||
Convert streams (readable or writable) to promises | ||
> Convert streams (readable or writable) to promises | ||
## Installing | ||
|
||
```sh | ||
$ npm install --save[-dev] 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 | ||
}) | ||
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'); | ||
writeableStream.end(); // promise is resolved here | ||
}) | ||
writableStream.write('data') | ||
writeableStream.end() // promise is resolved here | ||
``` | ||
|
||
### Error Handling | ||
|
||
```js | ||
var err = new Error(); | ||
var err = new Error() | ||
streamToPromise(stream).catch(function (error) { | ||
// error === err; | ||
}); | ||
stream.emit('error', err); // promise is rejected here | ||
// error === err | ||
}) | ||
stream.emit('error', err) // promise is rejected here | ||
``` |