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

pietile-promise-observer

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pietile-promise-observer

Simple Promise observer

  • 1.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by16.67%
Maintainers
1
Weekly downloads
 
Created
Source

Pietile Promise Observer

npm version install size

As Promise can't be canceled we can just unsubscribe from its result when don't need it.

Installation

Using yarn

yarn add pietile-promise-observer

or using npm

npm install -S pietile-promise-observer

Usage example

import { PromiseObserver, PromiseResult } from "pietile-promise-observer";

function asyncAction(): Promise<number> {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Math.random());
    }, 1000);
  });
}

function resultHandler(result: PromiseResult<number>): void {
  if (result.error) {
    // Smth wrong happened
    console.log("Error :(", result.error.message);
    return;
  }

  console.log(result.value + 1);
}

const promiseObserver = new PromiseObserver<number>();

promiseObserver.subscribe(asyncAction(), resultHandler);

// Somehwere later ...

promiseObserver.subscribe(asyncAction(), resultHandler);
// or
promiseObserver.unsubscribe();

API

new PromiseObserver()

Create new PromiseObserver.

subscribe(promise: Promise<T>, callback: Callback<T>, unsubscribedCallback?: Callback<T>): Promise<T>

Subscribe to promise. After the promise is resolved the callback will be called with either { value: null; error: Error; } or { value: T; error: null; } argument. Optional unsubscribedCallback will be called for unsubscribed promises. Return the same promise.

unsubscribe(): void

Unsubscribe from subscribed Promise

isSubscribed(): boolean

Is observer awaiting for any promise result?

PromiseObserver.WARN_ON_ERROR

Static property. When true will warn in console on each rejection. Useful for debugging

License

Pietile Promise Observer is MIT License.

Keywords

FAQs

Package last updated on 15 Feb 2021

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