{"action":"created","issue":{"url":"https://api.github.com/repos/m0ppers/promise-any/issues/1","labels_url":"https://api.github.com/repos/m0ppers/promise-any/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/m0ppers/promise-any/issues/1/comments","events_url":"https://api.github.com/repos/m0ppers/promise-any/issues/1/events","html_url":"https://github.com/m0ppers/promise-any/issues/1","id":99127006,"number":1,"title":"This version has an issue when there are errors thrown","user":{"login":"TimeForAChange","id":199179,"avatar_url":"https://avatars.githubusercontent.com/u/199179?v=3","gravatar_id":"","url":"https://api.github.com/users/TimeForAChange","html_url":"https://github.com/TimeForAChange","followers_url":"https://api.github.com/users/TimeForAChange/followers","following_url":"https://api.github.com/users/TimeForAChange/following{/other_user}","gists_url":"https://api.github.com/users/TimeForAChange/gists{/gist_id}","starred_url":"https://api.github.com/users/TimeForAChange/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TimeForAChange/subscriptions","organizations_url":"https://api.github.com/users/TimeForAChange/orgs","repos_url":"https://api.github.com/users/TimeForAChange/repos","events_url":"https://api.github.com/users/TimeForAChange/events{/privacy}","received_events_url":"https://api.github.com/users/TimeForAChange/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-05T05:45:50Z","updated_at":"2015-08-05T07:00:17Z","closed_at":null,"body":"I already had a version of \"any\" that I made myself but I tried yours because it's shorter. However, I went back to my own version because yours has an issue when an error is thrown - it doesn't show it, instead I got an error from one of the failed promises that went into \"any\".\r\n\r\nHere's my use case:\r\n\r\nI have files named after their own SHA256 and I don't know which directory they are in. So I simply start the I/O operation (read or delete) on _all_ directories and use the one that succeeds.\r\n\r\nBut before returning the contents of the file I add a little check. If I have an error in that `then(...)` I will see a \"file not found\" error from one of the failed promises instead of the error I throw.\r\n\r\n```javascript\r\nmodule.exports.getHash = function (hash) {\r\n    // Try all directories - see if any one attempt succeeds. We fail only if we\r\n    // can't find the file anywhere.\r\n    return any(Object.keys(STORAGE_DIR).map(type => {\r\n        return readFile(\r\n            path.join(STORAGE_DIR[type], hash)\r\n        )\r\n        .then(txt => {\r\n            // Before returning the contents of the file verify its crypto-hash\r\n            const verify = crypto\r\n                .createHash('sha256')\r\n                .update(txt, 'utf8')\r\n                .digest('hex');\r\n\r\n            if (hash !== verify) {\r\n                throw new Error(`Crypto-hash verification of file stored as ${hash} failed, actual hash is ${verify}`);\r\n            }\r\n\r\n            return txt;\r\n        });\r\n    }));\r\n};\r\n```\r\n\r\nIt works fine with my much more explicit version of \"any\":\r\n\r\n```javascript\r\nconst any = function (promises) {\r\n    return new Promise((/**Function*/ resolve, /**Function*/ reject) => {\r\n        let countDown = promises.length;\r\n\r\n        promises.map(promise => {\r\n            return promise\r\n            .then(resolve)\r\n            .catch(err => {\r\n                countDown -= 1;\r\n                if (countDown === 0) {\r\n                    reject(err);\r\n                }\r\n            });\r\n        });\r\n    });\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/m0ppers/promise-any/issues/comments/127895550","html_url":"https://github.com/m0ppers/promise-any/issues/1#issuecomment-127895550","issue_url":"https://api.github.com/repos/m0ppers/promise-any/issues/1","id":127895550,"user":{"login":"m0ppers","id":819421,"avatar_url":"https://avatars.githubusercontent.com/u/819421?v=3","gravatar_id":"","url":"https://api.github.com/users/m0ppers","html_url":"https://github.com/m0ppers","followers_url":"https://api.github.com/users/m0ppers/followers","following_url":"https://api.github.com/users/m0ppers/following{/other_user}","gists_url":"https://api.github.com/users/m0ppers/gists{/gist_id}","starred_url":"https://api.github.com/users/m0ppers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m0ppers/subscriptions","organizations_url":"https://api.github.com/users/m0ppers/orgs","repos_url":"https://api.github.com/users/m0ppers/repos","events_url":"https://api.github.com/users/m0ppers/events{/privacy}","received_events_url":"https://api.github.com/users/m0ppers/received_events","type":"User","site_admin":false},"created_at":"2015-08-05T07:00:17Z","updated_at":"2015-08-05T07:00:17Z","body":"Hi....i am not yet 100% sure if i understood your problem correctly. I have some test code made up:\r\n\r\n```\r\nvar test = function() {\r\n    return any([\r\n        Promise.reject('fail')\r\n    ], [\r\n        Promise.resolve('ok').then(\r\n            function() { throw new Error('Throw'); }\r\n      )\r\n    ]);\r\n}\r\n```\r\n\r\nIs this a minimal testcase for your problem? If so i don't really get the problem about it. None of the promises succeeded so it is returning one of the errors (the first). if you change the order of the promises you will get the \"throw\" error.\r\n\r\nYour version however is simply returning the error of the last promise :S\r\n\r\nAs to your use case i think your promise setup is simply wrong :o \r\n\r\nwouldn't it make more sense to do an exists check wrapped in any() to first determine the correct location and then continue working on the correct location?"}}