pull-thenable

Convert a pull-stream into a thenable

MIT License

Downloads
427
Stars
8
Committers
1

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')

async function main() {
  const source = thenable(pull.values(['a','b']));
  while (true) {
    try {
      const x = await source;
      console.log(x); // 'a'
                      // 'b'
    } catch (errOrEnd) {
      console.log(errOrEnd); // true
      return;
    }
  }
}

main();

License

MIT