Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-race

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-race - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

.editorconfig

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'))

2

package.json
{
"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)
})
})
})
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc