├── .gitignore ├── .npmignore ├── LICENSE ├── index.js ├── package.json ├── readme.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /shrinkwrap.yaml 2 | /package-lock.json 3 | /node_modules 4 | .DS_Store 5 | pnpm-debug.log 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /shrinkwrap.yaml 2 | /package-lock.json 3 | /node_modules 4 | .gitignore 5 | .DS_Store 6 | readme.md 7 | test.js -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 André Staltz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const thenable = require('pull-thenable'); 2 | 3 | module.exports = async function* awaitable(readable) { 4 | while (true) { 5 | try { 6 | yield await thenable(readable); 7 | } catch (err) { 8 | if (err === true) return; 9 | else throw err; 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pull-awaitable", 3 | "version": "1.0.0", 4 | "description": "Convert a pull-stream to an Async Iterable", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tape test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+ssh://git@github.com/staltz/pull-awaitable.git" 12 | }, 13 | "keywords": [ 14 | "pull-stream", 15 | "async", 16 | "async-iterable", 17 | "for-await-of", 18 | "async-await" 19 | ], 20 | "author": "staltz.com", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/staltz/pull-awaitable/issues" 24 | }, 25 | "homepage": "https://github.com/staltz/pull-awaitable", 26 | "dependencies": { 27 | "pull-thenable": "~1.0.0" 28 | }, 29 | "devDependencies": { 30 | "pull-stream": "^3.6.8", 31 | "tape": "^4.9.0" 32 | } 33 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # pull-awaitable 2 | 3 | > Converts a pull-stream to an Async Iterable, usable with for-await-of 4 | 5 | ```bash 6 | npm install --save pull-awaitable 7 | ``` 8 | 9 | **NOTE!** AsyncIterables and `for-await-of` are only supported in in Node.js v10, Firefox 57+, and Chrome 66. 10 | 11 | ## Usage 12 | 13 | ```js 14 | const pull = require('pull-stream') 15 | const awaitable = require('pull-awaitable') 16 | 17 | async function main() { 18 | const stream = pull.values(['a', 'b']); 19 | for await (const x of awaitable(stream)) { 20 | console.log(x); // 'a' 21 | // 'b' 22 | } 23 | } 24 | 25 | main() 26 | ``` 27 | 28 | ## License 29 | [MIT](https://tldrlegal.com/license/mit-license) 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var pull = require('../pull-stream'); 2 | var test = require('tape'); 3 | var awaitable = require('./index'); 4 | 5 | function delay() { 6 | return pull.asyncMap(function(x, cb) { 7 | setTimeout(function() { 8 | cb(null, x); 9 | }, 100); 10 | }); 11 | } 12 | 13 | test('consume 4 sync values with for-await-of', async function(t) { 14 | var expected = [10, 20, 30, 40]; 15 | var readable = pull(pull.values([1, 2, 3, 4]), pull.map(x => x * 10)); 16 | for await (var x of awaitable(readable)) { 17 | const e = expected.shift(); 18 | t.equals(x, e, 'should be ' + e); 19 | } 20 | t.end(); 21 | }); 22 | 23 | test('consume 4 async values with for-await-of', async function(t) { 24 | var expected = [10, 20, 30, 40]; 25 | var readable = pull( 26 | pull.values([1, 2, 3, 4]), 27 | delay(), 28 | pull.map(x => x * 10), 29 | ); 30 | for await (var x of awaitable(readable)) { 31 | const e = expected.shift(); 32 | t.equals(x, e, 'should be ' + e); 33 | } 34 | t.end(); 35 | }); 36 | 37 | test('propagates error with for-await-of', async function(t) { 38 | var expected = [10, 20]; 39 | var readable = pull( 40 | pull.values([1, 2, 3, 4]), 41 | pull.asyncMap((x, cb) => { 42 | if (x === 3) cb(new Error('bad stuff')); 43 | else cb(null, x); 44 | }), 45 | pull.map(x => x * 10), 46 | ); 47 | try { 48 | for await (var x of awaitable(readable)) { 49 | const e = expected.shift(); 50 | t.equals(x, e, 'should be ' + e); 51 | } 52 | } catch (err) { 53 | t.equals(err.message, 'bad stuff', 'error says "bad stuff"'); 54 | t.equals(expected.length, 0, 'no more values expected to come'); 55 | t.end(); 56 | } 57 | }); 58 | --------------------------------------------------------------------------------