promise-race
Advanced tools
Comparing version 1.0.0 to 1.0.1
29
index.js
@@ -0,7 +1,19 @@ | ||
/** | ||
* Class representing a PromiseRace | ||
*/ | ||
export default class PromiseRace { | ||
/** | ||
* Create a PromiseRace | ||
*/ | ||
constructor () { | ||
this.last = null | ||
this.lastID = null | ||
Object.assign(this, { | ||
last: null, | ||
lastID: null | ||
}) | ||
} | ||
/** | ||
* Returns true if the ID passed matches the ID of the last promise appended | ||
* @return {Boolean} | ||
*/ | ||
isLastID (id) { | ||
@@ -11,2 +23,6 @@ return id === this.lastID | ||
/** | ||
* Create a unique identifier to identify each promise | ||
* @return {String} | ||
*/ | ||
createID () { | ||
@@ -18,2 +34,7 @@ const id = Math.random().toString(36).substr(2, 10) | ||
/** | ||
* Add a promise to the race | ||
* @param {Promise} promise | ||
* @return {Promise} | ||
*/ | ||
append (promise) { | ||
@@ -37,2 +58,6 @@ if (!(promise instanceof Promise)) { | ||
/** | ||
* Return a promise that resolves when the last promise called fires | ||
* @return {Promise} | ||
*/ | ||
resolved () { | ||
@@ -39,0 +64,0 @@ return this.last || Promise.reject(Error('No promises have been received')) |
{ | ||
"name": "promise-race", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "An ES6 class to manage Promise race conditions. Will resolve to the first resolved Promise", | ||
@@ -5,0 +5,0 @@ "module": "index.js", |
@@ -5,14 +5,33 @@ import { expect } from 'chai' | ||
describe('PromiseRace', () => { | ||
it('resolves to the first resolved promise', async () => { | ||
const queue = new PromiseRace() | ||
const p1 = new Promise(resolve => setTimeout(resolve(1), 1000)) | ||
const p2 = new Promise(resolve => setTimeout(resolve(2), 500)) | ||
queue.append(p1) | ||
queue.append(p2) | ||
describe('append()', () => { | ||
it('resolves when the last promise appended resolves', async () => { | ||
const race = new PromiseRace() | ||
const result = await queue.resolved() | ||
expect(result).to.equal(2) | ||
const p1 = new Promise(resolve => setTimeout(resolve(1), 250)) | ||
const p2 = new Promise(resolve => setTimeout(resolve(2), 500)) | ||
let foo = 0 | ||
race.append(p1).then(() => { foo = 1 }) | ||
race.append(p2).then(() => { foo = 2 }) | ||
await race.resolved() | ||
expect(foo).to.equal(2) | ||
}) | ||
}) | ||
describe('resolved()', () => { | ||
it('will resolve when the last promise appended resolves', async () => { | ||
const race = new PromiseRace() | ||
const p1 = new Promise(resolve => setTimeout(resolve(1), 250)) | ||
const p2 = new Promise(resolve => setTimeout(resolve(2), 500)) | ||
race.append(p1) | ||
race.append(p2) | ||
const result = await race.resolved() | ||
expect(result).to.equal(2) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64549
10
86
35