├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── readme.md ├── index.js ├── package.json ├── license └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/observable-to-promise 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: actions/setup-node@v3 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # observable-to-promise 2 | 3 | > Convert an [Observable](https://github.com/tc39/proposal-observable) to a Promise 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install observable-to-promise 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import observableToPromise from 'observable-to-promise'; 15 | 16 | const promise = observableToPromise(Observable.of(1, 2)); 17 | 18 | console.log(await promise); 19 | //=> [1, 2] 20 | ``` 21 | 22 | ## Related 23 | 24 | - [is-observable](https://github.com/sindresorhus/is-observable) - Check if a value is an Observable 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import isObservable from 'is-observable'; 2 | import symbolObservable from 'symbol-observable'; 3 | 4 | export default async function observableToPromise(value) { 5 | if (!isObservable(value)) { 6 | throw new TypeError(`Expected an \`Observable\`, got \`${typeof value}\``); 7 | } 8 | 9 | const values = []; 10 | 11 | return new Promise((resolve, reject) => { 12 | value[symbolObservable.default]().subscribe({ 13 | next(value) { 14 | values.push(value); 15 | }, 16 | error: reject, 17 | complete() { 18 | resolve(values); 19 | }, 20 | }); 21 | }); 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "observable-to-promise", 3 | "version": "2.0.0", 4 | "description": "Convert an Observable to a Promise", 5 | "license": "MIT", 6 | "repository": "sindresorhus/observable-to-promise", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=18" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "observable", 26 | "observables", 27 | "promise", 28 | "promises", 29 | "convert", 30 | "subscribe" 31 | ], 32 | "dependencies": { 33 | "is-observable": "^3.0.0", 34 | "symbol-observable": "^4.0.0" 35 | }, 36 | "devDependencies": { 37 | "ava": "^5.3.1", 38 | "most": "^1.9.0", 39 | "p-is-promise": "^4.0.0", 40 | "rxjs": "^7.8.1", 41 | "xo": "^0.56.0", 42 | "xstream": "^11.14.0", 43 | "zen-observable": "^0.9.0" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import pIsPromise from 'p-is-promise'; 3 | import ZenObservable from 'zen-observable'; 4 | import xs from 'xstream'; 5 | import {from as rxFrom} from 'rxjs'; 6 | import * as most from 'most'; 7 | import toPromise from './index.js'; 8 | 9 | // Run tests for a given observable library 10 | function testWithALib([libraryName, fromArray, failed]) { 11 | const fixture = [1, 2]; 12 | 13 | test(`${libraryName}: observable to promise`, t => { 14 | t.true(pIsPromise(toPromise(fromArray(fixture)))); 15 | }); 16 | 17 | test(`${libraryName}: passes values through`, async t => { 18 | t.deepEqual(await toPromise(fromArray(fixture)), fixture); 19 | }); 20 | 21 | test(`${libraryName}: rejects on error in observable`, async t => { 22 | await t.throwsAsync(toPromise(failed())); 23 | }); 24 | } 25 | 26 | // Run tests for the list of libraries 27 | function testWithLibs(libraries) { 28 | for (const library of libraries) { 29 | testWithALib(library); 30 | } 31 | } 32 | 33 | // Run tests not using any observables 34 | function commonTests() { 35 | test('throw an error when an non-observable is given', async t => { 36 | await t.throwsAsync(toPromise(2), {instanceOf: TypeError}); 37 | }); 38 | } 39 | 40 | commonTests(); 41 | 42 | // Prepare constructors for each library 43 | const reason = new Error('Rejected for testing'); 44 | 45 | const rejected = async () => { 46 | throw reason; 47 | }; 48 | 49 | const zenFrom = array => ZenObservable.from(array); 50 | const zenFailed = () => new ZenObservable(observer => observer.error(reason)); 51 | 52 | const xsFrom = array => xs.default.from(array); 53 | const xsFailed = () => xs.default.fromPromise(rejected()); 54 | 55 | const rxFailed = () => rxFrom(rejected()); 56 | 57 | const mostFrom = array => most.from(array); 58 | const mostFailed = () => most.fromPromise(rejected()); 59 | 60 | // Run tests with prepared constructors 61 | testWithLibs([ 62 | ['zen-observable', zenFrom, zenFailed], 63 | ['xstream', xsFrom, xsFailed], 64 | ['RxJS 5', rxFrom, rxFailed], 65 | ['most', mostFrom, mostFailed], 66 | ]); 67 | --------------------------------------------------------------------------------