🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@jcoreio/once-async

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@jcoreio/once-async

async version of lodash.once that avoids retained promise memory leaks

unpublished
latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
0
Created
Source

@jcoreio/once-async

async version of lodash.once that avoids retained promise memory leaks

CircleCI Coverage Status semantic-release npm version

The Problem

The following onceified function leaks memory:

import once from 'lodash/once'
export const getOnce = once(async () => ...)

The first call creates a Promise that is retained forever; and then/catch/finally handlers registered on this Promise will thus also be retained forever, as well as any objects in the closure of those handlers, causing the memory leak.

The Solution

All we need to do is discard the Promise once it's fulfilled, keeping only the resolved value or rejection reason for future function calls.

API

Example:

import { onceAsync } from '@jcoreio/once-async'

const fetchOnce = onceAsync(async () =>
  fetch('https://com.com').then((res) => res.json())
)

The created function calls your function once with the arguments and this binding of the first call to the created function. Subsequent calls will return a promise that resolves with the same value or rejects with the same reason as the promise returned by the created function.

Keywords

once

FAQs

Package last updated on 17 Dec 2024

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