Socket
Socket
Sign inDemoInstall

@yume-chan/async-operation-manager

Package Overview
Dependencies
1
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @yume-chan/async-operation-manager

Utilities to manage RPC operations.


Version published
Maintainers
1
Install size
54.8 kB
Created

Changelog

Source

2.1.4

  • PromiseResolver#resolve and PromiseResolver#reject are automatically bind to instance.

Readme

Source

Async Operation Manager

This package contains utilities to manage RPC operations.

  • Async Operation Manager

PromiseResolver

export type PromiseResolverState = 'running' | 'resolved' | 'rejected';

export class PromiseResolver<T> {
    readonly promise: Promise<T>;
    readonly state: PromiseResolverState;

    resolve(value?: T | PromiseLike<T>): void;
    reject(reason?: any): void;
}

C#, the origin of the async/await pattern, uses TaskCompletionSource to manage Tasks manually.

V8, the most commonly used JavaScript engine, uses PromiseResolver to manage Promises manually.

But in JavaScript, we only have the Promise constructor, and there is no way to know the Promise's state.

Now you can use this PromiseResolver class to create and manage Promises more conveniently.

function delay(timeout: number): Promise<void> {
    const resolver = new PromiseResolver<void>();
    setTimeout(() => resolver.resolve(), timeout);
    return resolver.promise;
}

AsyncOperationManager

export interface AsyncOperationInfo<T> {
    id: number;
    promise: Promise<T>;
}

export default class AsyncOperationManager {
    add<T>(): AsyncOperationInfo<T>;

    resolve<T>(id: number, result: T): void;
    reject(id: number, reason: Error): void;
}

Assume you have an RPC service, every operation has an ID, and the remote will return the result with this ID.

AsyncOperationManager can help you manage the IDs and convert callbacks to Promises.

declare const MyService;
const manager = new AsyncOperationManager();

MyService.on('complete', (id: number, result: number) => {
    manager.resolve(id, result);
});

MyService.on('error', (id: number, message: string) => {
    manager.reject(id, new Error(message));
});

function callService(payload: number): Promise<number> {
    const { id, promise } = manager.add<number>();
    MyService.post({ id, payload });
    return promise;
}

Keywords

FAQs

Last updated on 20 Jan 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc