├── .gitignore ├── index.js ├── README.md ├── package.json ├── UNLICENSE └── test └── any.js /.gitignore: -------------------------------------------------------------------------------- 1 | .*\.swp 2 | node_modules 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function reverse(promise) { 4 | return new Promise((resolve, reject) => Promise.resolve(promise).then(reject, resolve)); 5 | } 6 | 7 | module.exports = function promiseAny(iterable) { 8 | return reverse(Promise.all([...iterable].map(reverse))); 9 | }; 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # promise-any 2 | 3 | [ ![Codeship Status for m0ppers/promise-any](https://codeship.com/projects/82cc13e0-f2e0-0131-e5b7-4a729fc31f8d/status?branch=master)](https://codeship.com/projects/27744) 4 | 5 | ES6 promises don't provide any(). A small library to implement them. Will convert arguments to promises if not already a promise. 6 | 7 | ```javascript 8 | var promiseAny = require('promise-any'); 9 | 10 | promiseAny([ 11 | Promise.reject('✗'), 12 | Promise.resolve('✓'), 13 | ]).then(function(value) { 14 | // value is '✓' :) 15 | }); 16 | 17 | promiseAny([ 18 | Promise.reject('✗'), 19 | Promise.reject('✗'), 20 | ]).catch(function(reasons) { 21 | // reasons is ['✗', '✗'] :( 22 | }); 23 | ``` 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promise-any", 3 | "version": "0.2.0", 4 | "description": "returns first successful promise. As in Promise.any() which is not part of the ES6 spec", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/m0ppers/promise-any.git" 12 | }, 13 | "keywords": [ 14 | "promise", 15 | "es6", 16 | "any", 17 | "resolve" 18 | ], 19 | "author": "Andreas Streichardt ", 20 | "license": "Unlicense", 21 | "bugs": { 22 | "url": "https://github.com/m0ppers/promise-any/issues" 23 | }, 24 | "engines" : { "node" : ">=5.0.0" }, 25 | "homepage": "https://github.com/m0ppers/promise-any#readme", 26 | "devDependencies": { 27 | "mocha": "^2.2.5" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /test/any.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var promiseAny = require('../index'); 3 | var assert = require('assert'); 4 | 5 | describe('Any test', function () { 6 | 7 | it('should handle []', function (done) { 8 | let promises = []; 9 | promiseAny(promises).then(function (value) { 10 | done(value); 11 | }, function (reasons) { 12 | assert.deepEqual([], reasons); 13 | done(); 14 | }); 15 | }); 16 | 17 | it('should handle [success, failure]', function (done) { 18 | let promises = [Promise.resolve('p1'), Promise.reject('p2')]; 19 | promiseAny(promises).then(function(value) { 20 | assert.equal(value, 'p1'); 21 | done(); 22 | }, function (reasons) { 23 | done(reasons); 24 | }); 25 | }); 26 | 27 | it('should handle [failure, success]', function (done) { 28 | let promises = [Promise.reject('p1'), Promise.resolve('p2')]; 29 | promiseAny(promises).then(function (value) { 30 | assert.equal(value, 'p2'); 31 | done(); 32 | }, function (reasons) { 33 | done(reasons); 34 | }); 35 | }); 36 | 37 | it('should handle [failure, failure]', function (done) { 38 | let promises = [Promise.reject('p1'), Promise.reject('p2')]; 39 | promiseAny(promises).then(function (value) { 40 | done('Got value ' + value); 41 | }, function (reasons) { 42 | assert.deepEqual(reasons, ['p1', 'p2']); 43 | done(); 44 | }); 45 | }); 46 | 47 | it('should handle any iterables', function (done) { 48 | let promises = (function *() { 49 | yield Promise.reject('p1'); 50 | yield Promise.resolve('p2'); 51 | })(); 52 | promiseAny(promises).then(function(value) { 53 | assert.equal(value, 'p2'); 54 | done(); 55 | }, function (reasons) { 56 | done(reasons); 57 | }); 58 | }); 59 | 60 | it('should handle value that is not a promise', function (done) { 61 | let promises = ['p1'] 62 | promiseAny(promises).then(function (value) { 63 | assert.equal(value, 'p1'); 64 | done(); 65 | }, function (reasons) { 66 | done(reasons); 67 | }) 68 | }); 69 | 70 | }); 71 | --------------------------------------------------------------------------------