Socket
Socket
Sign inDemoInstall

promise-rat-race

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.4.0

.eslintrc.js

8

package.json
{
"name": "promise-rat-race",
"version": "1.0.0",
"version": "1.4.0",
"description": "like Promise.race, but doesn't care about the losers in the race",

@@ -24,5 +24,7 @@ "main": "rat-race.js",

"devDependencies": {
"next-build-tools": "^5.17.0",
"origami-build-tools": "^5.0.0"
"chai": "^3.5.0",
"eslint": "^2.2.0",
"lintspaces-cli": "^0.1.1",
"mocha": "^2.5.3"
}
}
'use strict';
module.exports = promises => {
const start = Date.now();
function resolver (promise) {
return promise.then(data => {
const taken = Date.now()-start;
return { state: 'resolve', data, taken };
}, data => {
return { state: 'reject', data };
return new Promise((resolve, reject) => {
let resolved = false;
let firstError;
Promise.all(promises.map(promise => {
return promise
.then(result => {
if (!resolved) {
resolve(result);
resolved = true;
}
}, err => {
firstError = firstError || err;
})
}))
.then(() => {
if (firstError && !resolved) {
reject(firstError);
}
});
}
return Promise.all(promises.map(promise => resolver(promise)))
.then(results => {
const candidates = results.filter(result => result.state === 'resolve');
if (candidates.length > 0) {
return candidates.sort((a, b) => {
if (a.taken < b.taken) {
return -1;
}
if (a.taken > b.taken) {
return 1;
}
return 0;
})[0].data;
}
return Promise.reject(results.map(result => result.data));
});
});
};

@@ -14,3 +14,1 @@ # promise-rat-race

```
**Currently no tests, and only loosely maintained - use at your own risk... or submit a PR with tests, linting etc. :)**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc