New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pendings

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pendings

Better control of pending promises

  • 0.2.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

Pendings

Build Status npm license

Better control of pending Promises

Pendings is a library for control of pending promises. It manages resolve / reject callbacks and provides convenient access to them for promise fulfillment.

Installation

npm install pendings --save

Usage (single promise)

Typical situation with promises in event-based code:

class Foo {
    constructor() {
      this.promise = null;
      this.resolve = null;
      this.reject = null;
    }
    asyncRequest() {
        if (this.promise) { // if promise already exists - return it
            return this.promise;
        }
        this.promise = new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
            this.send();
        });
        return this.promise;
    }
    onSuccess(data) {
        this.resolve(data);
    }
}    

Pending class allows to do it simpler:

const Pending = require('pendings').Pending;

class Foo {
    constructor() {
        this.pending = new Pending();
    }
    asyncRequest() { 
        return this.pending.call(() => this.send());
    }
    onSuccess(data) {
        this.pending.resolve(data);
    }
}

Usage (list of promises)

Pendings class is useful for dynamic list of promises. Each promise can automatically get unique id and can be fulfilled later by that id. After fulfillment promise is removed from list.

const Pendings = require('pendings');

class Foo {
    constructor() {
        this.pendings = new Pendings();
    }    

    asyncRequest() { 
        return this.pendings.add(id => {
            this.send({id, foo: 'bar'}); // mark request with unique `id` generated by Pendings
        });
    }
    
    onSuccess(data) {
        this.pendings.resolve(data.id, data); // resolve by `id`
    }
    
    onError(data) {
        this.pendings.reject(data.id, data); // reject by `id`
    }
}

API

Classes

Pending
Pendings
TimeoutError

Pending

Kind: global class

new Pending()

Creates instance of single pending promise. It holds resolve / reject callbacks for future fulfillment.

pending.promise ⇒ Promise

Returns promise itself.

Kind: instance property of Pending

pending.isResolved ⇒ Boolean

Returns true if promise resolved.

Kind: instance property of Pending

pending.isRejected ⇒ Boolean

Returns true if promise rejected.

Kind: instance property of Pending

pending.isFulfilled ⇒ Boolean

Returns true if promise fulfilled (resolved or rejected).

Kind: instance property of Pending

pending.onFulfilled

Callback called when promise is fulfilled (resolved or rejected).

Kind: instance property of Pending

ParamType
fnfunction

pending.call(fn, [timeout]) ⇒ Promise

For the first time this method calls fn and returns new promise. Also holds resolve / reject callbacks to allow fulfill promise via pending.resolve() and pending.reject(). All subsequent calls of .call(fn) will return the same promise, which can be still pending or already fulfilled. To reset this behavior use .reset(). If timeout is specified, the promise will be automatically rejected after timeout milliseconds with PendingTimeoutError.

Kind: instance method of Pending

ParamTypeDefault
fnfunction
[timeout]Number0

pending.resolve([value])

Resolves pending promise with specified value.

Kind: instance method of Pending

ParamType
[value]*

pending.reject([reason])

Rejects pending promise with specified reason.

Kind: instance method of Pending

ParamType
[reason]*

pending.fulfill([value], [reason])

Helper method: rejects if reason is truthy, otherwise resolves with value.

Kind: instance method of Pending

ParamType
[value]*
[reason]*

pending.reset()

Resets to initial state.

Kind: instance method of Pending

Pendings

Kind: global class

new Pendings([options])

Creates dynamic list of promises. When each promise if fulfilled it is remove from list.

ParamTypeDefaultDescription
[options]Object
[options.idPrefix]String''prefix for generated IDs
[options.timeout]Number0default timeout for all promises

pendings.add(fn, [options]) ⇒ Promise

Calls fn and returns new promise. fn gets generated unique id as parameter.

Kind: instance method of Pendings

ParamTypeDescription
fnfunction
[options]Object
[options.timeout]Numbercustom timeout for particular promise

pendings.set(id, fn, [options]) ⇒ Promise

Calls fn and returns new promise with specified id. If promise with such id already pending - it will be returned.

Kind: instance method of Pendings

ParamTypeDescription
idString
fnfunction
[options]Object
[options.timeout]Numbercustom timeout for particular promise

pendings.has(id) ⇒ Boolean

Checks if promise with specified id is pending.

Kind: instance method of Pendings

ParamType
idString

pendings.resolve(id, [value])

Resolves pending promise by id with specified value. Throws if promise does not exist or is already fulfilled.

Kind: instance method of Pendings

ParamType
idString
[value]*

pendings.reject(id, [reason])

Rejects pending promise by id with specified reason. Throws if promise does not exist or is already fulfilled.

Kind: instance method of Pendings

ParamType
idString
[reason]*

pendings.fulfill(id, [value], [reason])

Rejects pending promise by id if reason is truthy, otherwise resolves with value. Throws if promise does not exist or is already fulfilled.

Kind: instance method of Pendings

ParamType
idString
[value]*
[reason]*

pendings.tryResolve(id, [value])

Resolves pending promise by id with specified value if it exists.

Kind: instance method of Pendings

ParamType
idString
[value]*

pendings.tryReject(id, [reason])

Rejects pending promise by id with specified reason if it exists.

Kind: instance method of Pendings

ParamType
idString
[reason]*

pendings.tryFulfill(id, [value], [reason])

Rejects pending promise by id if reason is truthy, otherwise resolves with value.

Kind: instance method of Pendings

ParamType
idString
[value]*
[reason]*

pendings.rejectAll([reason])

Rejects all pending promises with specified reason. Useful for cleanup.

Kind: instance method of Pendings

ParamType
[reason]*

pendings.generateId() ⇒ String

Generates unique ID. Can be overwritten.

Kind: instance method of Pendings

TimeoutError

Kind: global class

new TimeoutError(timeout)

Timeout error for pending promise.

ParamType
timeoutNumber

License

MIT @ Vitaliy Potapov

Keywords

FAQs

Package last updated on 11 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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