pull-thenable

1.0.0 • Public • Published

pull-thenable

Converts a pull-stream to an object with .then(), usable also with async-await

npm install --save pull-thenable

Usage

As a chain of then's:

const pull = require('pull-stream')
const thenable = require('pull-thenable')

const stream = pull.values(['a','b']);
const source = thenable(stream);

source.then(x => {
  console.log(x); // 'a'
  return source;
}).then(x => {
  console.log(x); // 'b'
  return source;
}).then(x => {
  // not called
}, err => {
  console.log(err); // true
})

With async-await:

const pull = require('pull-stream')
const thenable = require('pull-thenable')

const stream = pull.values(['a','b']);
const source = thenable(stream);

async function main() {
  while (true) {
    try {
      const x = await thenable(readable);
      console.log(x); // 'a'
                      // 'b'
    } catch (errOrEnd) {
      console.log(errOrEnd); // true
      return;
    }
  }
}

main();

License

MIT

Package Sidebar

Install

npm i pull-thenable

Weekly Downloads

85

Version

1.0.0

License

MIT

Unpacked Size

3.41 kB

Total Files

4

Last publish

Collaborators

  • staltz