npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yume-chan/async

v2.2.0

Published

Utils for async programming.

Downloads

4,242

Readme

Async

license GitHub Workflow Status Codecov npm version npm type definitions

Utils for async programming.

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