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.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 provides better control of pending Promises by automatic managing resolve / reject callbacks.

Installation

npm install pendings --save

Example

In promise-based class sometimes we store resolve / reject callbacks for future promise fulfillment. E.g.:

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 library allows to simplify it:

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);
    }
}

Moreover, it is useful for list of pending promises. The unique id is generated for each promise and allows to fulfill it:

const Pendings = require('pendings');

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

    sendDataAndWait(data) { 
        return this.pendings.add(id => {
            data.id = id;
            this.send(data);
        });
    }
    
    onEvent(event) {
        const data = event.data;
        this.pendings.resolve(data.id, data);
    }
    
    onError(event) {
        const data = event.data;        
        this.pendings.reject(data.id, data);
    }
}

API

  • Pendings - controls list of promises
    • .add(fn) - calls fn and returns new promise. fn gets unique id as parameter
    • .set(id, fn) - calls fn and returns new promise with specified id
    • .has(id) - does promise with specified id exist
    • .resolve(id, value) - resolves pending promise by id with specified value
    • .reject(id, reason) - rejects pending promise by id with specified reason
    • .fulfill(id, reason) - rejects pending promise if reason is specified, otherwise resolves with empty value
  • Pending - controls single promise
    • .call(fn) - calls fn and returns new promise
    • .resolve(value) - resolves pending promise
    • .reject(reason) - reject pending promise
    • .fulfill(reason) - rejects pending promise if reason is specified, otherwise resolves with empty value

License

MIT @ Vitaliy Potapov

Keywords

FAQs

Package last updated on 23 Jun 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