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.1.10
  • 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

Example

When working with events in promise-based code we need to manually store resolve / reject callbacks for future fulfillment:

class MyClass {
    waitSomeEvent() { 
        return new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
            this.request();
        });
    }
    
    onEvent(event) {
        this.resolve(event.data);
    }
    
    onError(event) {
        this.reject(event.data);
    }
}

Pendings allows to reduce boilerplate code:

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

class MyClass {
    constructor() {
        this.pending = new Pending();
    }    

    waitSomeEvent() { 
        return this.pending.call(() => this.request());
    }
    
    onEvent(event) {
        this.pending.resolve(event.data);
    }
    
    onError(event) {
        this.pending.reject(event.data);
    }
}

This is even more useful for list of pending promises. Each promise automatically gets unique id that allows to fulfill it later:

const Pendings = require('pendings');

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

    sendDataAndWaitResponse(data) { 
        return this.pendings.add(id => { // `id` is generated by Pendings
            data.id = id;
            this.send(data);
        });
    }
    
    onEvent(event) {
        const data = event.data;
        this.pendings.resolve(data.id, data); // response event should contain `id` property
    }
    
    onError(event) {
        const data = event.data;        
        this.pendings.reject(data.id, data);
    }
}

API

Classes

Pendings
Pending

Pendings

Kind: global class

new Pendings([options])

Constructor.

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.

Kind: instance method of Pendings

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

pendings.has(id) ⇒ Boolean

Checks if pending promise with specified id exists.

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.

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.

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.

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.getPromise(id) ⇒ Promise | undefined

Returns promise of pending object with specified id.

Kind: instance method of Pendings

ParamType
idString

pendings.generateId() ⇒ String

Generates unique ID. Can be overwritten.

Kind: instance method of Pendings

Pending

Kind: global class

new Pending()

Constructor.

pending.promise ⇒ Promise

Returns promise itself.

Kind: instance property of Pending

pending.isFulfilled ⇒ Boolean

Returns is promise fulfilled or not.

Kind: instance property of Pending

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

Calls fn, returns new promise and holds resolve / reject callbacks.

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

Rejects if reason is truthy, otherwise resolves with value.

Kind: instance method of Pending

ParamType
[value]*
[reason]*

License

MIT @ Vitaliy Potapov

Keywords

FAQs

Package last updated on 05 Jul 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