stream-from-promise

WebJar for stream-from-promise

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

stream-from-promise
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

stream-from-promise
WebJar for stream-from-promise
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/schnittstabil/stream-from-promise

Download stream-from-promise

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/stream-from-promise/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>stream-from-promise</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/stream-from-promise/
implementation 'org.webjars.npm:stream-from-promise:1.0.0'
// https://jarcasting.com/artifacts/org.webjars.npm/stream-from-promise/
implementation ("org.webjars.npm:stream-from-promise:1.0.0")
'org.webjars.npm:stream-from-promise:jar:1.0.0'
<dependency org="org.webjars.npm" name="stream-from-promise" rev="1.0.0">
  <artifact name="stream-from-promise" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='stream-from-promise', version='1.0.0')
)
libraryDependencies += "org.webjars.npm" % "stream-from-promise" % "1.0.0"
[org.webjars.npm/stream-from-promise "1.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

stream-from-promise Dependencies Status Image Build Status Image Coverage Status

Create streams from ECMAScript 2015 Promises.

npm install stream-from-promise --save

Usage

String | Buffer promises

import StreamFromPromise from 'stream-from-promise';

const stringPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('strrrring!');
  }, 500);
});

StreamFromPromise(stringPromise)
  .pipe(process.stdout); // output: strrrring!


const bufferPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(new Buffer('buff!'));
  }, 500);
});

StreamFromPromise(bufferPromise)
  .pipe(process.stdout); // output: buff!

Arbitrary Promises

import StreamFromPromise from 'stream-from-promise';

const funcPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(() => {
      console.log('func!?!');
    });
  }, 500);
});

StreamFromPromise.obj(funcPromise)
  .on('data', fn => {
    fn(); // output: func!?!
  });

Rejecting

import StreamFromPromise from 'stream-from-promise';

const rejectPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject(new Error('rejected'));
  }, 500);
});

StreamFromPromise(rejectPromise)
  .on('error', err => {
    console.log(err); // output: [Error: rejected]
  })
  .on('data', data => {
    // do something awsome
  });

Gulp File promises

Gulp files are vinyl files:

npm install vinyl

Test some awsome Gulp plugin:

import StreamFromPromise from 'stream-from-promise';
import File from 'vinyl';

const hello = new File({
      cwd: '/',
      base: '/hello/',
      path: '/hello/hello.js',
      contents: new Buffer('console.log("Hello");')
    });

const helloFilePromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(hello);
  }, 500);
});

StreamFromPromise.obj(helloFilePromise)
  .pipe(someAwsomeGulpPlugin())
  .on('data', file => {
    console.log(file.contents.toString()); // dunno what someAwsomeGulpPlugin does :)
  });

See also stream-recorder for testing gulp plugins.

API

Class: StreamFromPromise

StreamFromPromises are Readable streams.

new StreamFromPromise(promise, [options])

  • promise Promise ECMAScript 2015 Promises returning Javascript values like numbers, strings, objects, functions, ...
  • options Object passed through new Readable([options])

Note: The new operator can be omitted.

StreamFromPromise#obj(promise, [options])

A convenience wrapper for new StreamFromPromise(promise, {objectMode: true, ...}).

License

MIT © Michael Mayer

Versions

Version
1.0.0