New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

coalescify

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

coalescify

coalesce inflight promise calls into a single promise

latest
Source
npmnpm
Version
1.0.5
Version published
Weekly downloads
7
75%
Maintainers
1
Weekly downloads
 
Created
Source

coalescify

coalesce inflight promise calls into a single promise

import coalescify from 'coalescify';

const fetchResource = async (id: number) => {
  // ...
};

const coalescedFetchResource = coalescify(fetchResource);

const results = await Promise.all([
    coalescedFetchResource(1), 
    coalescedFetchResource(1),
    coalescedFetchResource(1),
    coalescedFetchResource(3)
])

// coalescedFetchResource(1) will only be called once, and the resolved result will be shared between all other calls
// coalescedFetchResource(3) will only be called once

coalescify will automatically determine inflight calls to coalesce based on the arguments passed to the function. If the arguments are the same, the promise will be shared.

a custom coalescion key generator function can be passed as the second argument

import coalescify from 'coalescify';

const fetchResource = async (type: string, id: number) => {
  // ...
};

const coalescedFetchResource = coalescify(fetchResource, (type, id) => `${type}/${id}`);

const results = await Promise.all([
    coalescedFetchResource('dog', 1), 
    coalescedFetchResource('dog', 1),
    coalescedFetchResource('dog', 2),
    coalescedFetchResource('cat', 1)
])

// coalescedFetchResource('dog', 1) will be called once
// coalescedFetchResource('dog', 2) will be called once
// coalescedFetchResource('cat', 1) will be called once

using a custom coalescion key generator is generally faster, since the default implementation uses JSON.stringify on function arguments, which is slower

Keywords

async

FAQs

Package last updated on 28 Oct 2023

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