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.1
  • 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 their resolve | reject callbacks.

Installation

npm install pendings --save

Motivation

Imagine you are devepoling promise-based WebSocket wrapper. It has open() method that returns Promise and resolves when WebSocket onopen event comes. To achieve that you may store resolve | reject callbacks as class properties and fulfill them later:

class MyWebSocket {
    /**
     * @returns {Promise} 
     */
    open(url) { 
        return new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
            const ws = new WebSocket(url);
            ws.onopen = event => this.onWebSocketOpen(event);
            ws.onerror = event => this.onWebSocketError(event);
        });
    }
    
    onWebSocketOpen(event) {
        this.resolve(event);
    }
    
    onWebSocketError(event) {
        this.reject(event);
    }
}

Pendings library allows to automatically store resolve | reject callbacks:

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

class MyWebSocket {
    constructor() {
        this.pending = new Pending();
    }
    /**
     * @returns {Promise} 
     */
    open(url) { 
        return this.pending.call(() => {
            this.ws = new WebSocket(url);
            this.ws.onopen = event => this.onWebSocketOpen(event);
            this.ws.onerror = event => this.onWebSocketError(event);
        });
    }
    
    onWebSocketOpen(event) {
        this.pending.resolve(event);
    }
    
    onWebSocketError(event) {
        this.pending.reject(event);
    }
}

// usage
new MyWebSocket().open()
  .then(() => console.log('WebSocket opened'));

This is even more handy when you have several pending promises. Then each pending promise has own unique id that allows to resolve it when needed:

const Pendings = require('pendings');

class MyWebSocket {
    constructor() {
        this.pendings = new Pendings();
    }
    /**
     * @returns {Promise} 
     */
    send(data) { 
        return this.pendings.add(id => {
            data.id = id;
            this.ws.send(JSON.stringify(data));
        });
    }
    
    onWebSocketMessage(event) {
        const id = event.data.id;
        this.pendings.resolve(id, event.data);
    }
}

// usage
myWebSocket.send({foo: 'bar'})
  .then(data => console.log('response:', data)); 

API

  • Pendings - controls list of pending 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.
    • .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