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

single-promise

v0.1.0

Published

Resilient singleton promise to make sure it runs once and only once. Differently from most singleton promise implementations it retries if failed.

Downloads

24

Readme

single-promise

A simple but reliable singleton promise implementation (key-based). Useful when you want to load remote resources only once and wants to make sure that they are successfully executed

Installation

npm install single-promise

Usage

Each singleton promise is identified by a key. You can call it in different parts of your code and as long as it returned successfully once it wont't be called again.

The problem of most singleton promise implementations, is that they assume that things always work. But that's not the case. Sometimes things go wrong, mostly on async calls.

SinglePromise solves exactly these cases. It not only implements the singleton promise pattern, but it also makes sure that if the first call fails, the second time you call it, it will try to call the original promise again.

It is important to understand that a promise CANNOT be re-executed. That's why when calling promise one, we need to pass a function that returns a Promise. It should return each time a new promise.


// Assuming that initializeSomething return a promise, the below code will first resolve initializeSomething
// and then call doSomething
SinglePromise.resolve("init", () => initializeSomething()).then(() => doSomething());

(...)

// In some other part of your code, you need to doOtherThing, but before you have to make sure that
// initializeSomething is done. 3 scenario are possible here:
// 1. initializeSomething is still running. In this case it will wait for it to finish and then run doOtherThing
// 2. initializeSomething executed successfully. In this case it will execute doOtherThing "immediately"
// 3. initializeSomething failed. In this case it will execute initializeSomething again and then doOtherThing (if initialize something succeeds)
SinglePromise.resolve("init", () => initializeSomething()).then(() => doOtherThing());

Once a promise is registered, in the subsequent calls you don't need to pass the factory function again. As in the example below, the promise is first resolved in the class constructor. In the subsequent methods we don't need to pass the promise initialization again

class MyClass {
    private myLibPromise;

    constructor() {
        SinglePromise.resolve("my-lib", () => this.loadMyLib());
    }

    async loadMyLib() {
        // Load my library.
    }

    async callMyLib() {
        await SinglePromise.resolve("my-lib");
        // Use my library
    }
}