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

synchronized-node

v1.0.8

Published

Lib for synchronized functions call with node js

Downloads

5

Readme

synchronized-node

A simple and lightweight library to execute functions in a synchronized way, preventing concurrent calls to the same function.

Installation

To install the library, run:

npm install synchronized-node
//or
yarn add synchronized-node

Usage

To use the library, you need to import the executeSynchronized function from the module:

import { executeSynchronized } from "synchronized-node";
//or
import executeSynchronized from "synchronized-node";

Then, you can pass any function and its arguments to the executeSynchronized function, which will return a promise that resolves to the result of the function. For example:

import axios from "axios";

import { executeSynchronized } from "synchronized-node";

const getTime = async (): Promise<unknown> => {
  try {
    const {
      data: { hour, minute, seconds },
    } = await axios.get(
      "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
    );

    return {
      hour,
      minute,
      seconds,
    };
  } catch (error) {
    throw error;
  }
};

const test = async (number: number) => {
  const response = await executeSynchronized(getNumber);

  console.log(number, response);
};

Promise.all([test(3), test(1), test(3)]);

The executeSynchronized function will ensure that only one instance of the same function is running at a time. If another call to the same function is made while it is still running, it will wait until the previous call is finished before executing. This is useful for preventing race conditions or data corruption when dealing with shared resources.

The library uses the function name as the key to identify the function. Therefore, the function passed to the executeSynchronized function must have a name. Anonymous functions or arrow functions are not supported.

The library implements synchronization using semaphores, using the function name as the flag to lock and unlock the function. This means that you can execute multiple functions in a synchronized way, as long as they have different names. For example:

const test1 = async (number: number) => {
  // some async logic
  return number * 2;
};

const test2 = async (number: number) => {
  // some async logic
  return number * 3;
};

executeSynchronized(test1, 1);
executeSynchronized(test1, 2);

executeSynchronized(test2, 1);
executeSynchronized(test2, 2);

// the function test1 with value 2 will wait for the execution of the function test1 with value 1 to finish
// the function test2 with value 2 will wait for the execution of the function test2 with value 1 to finish
// the function test2 with any value will not wait for the execution of the function test1 with any value

License

This library is licensed under the MIT License. See the LICENSE file for more details.