You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

next-resolver

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-resolver

A utility to simpify the repeated *unique ID* + *promise* dance

0.1.1
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

next-resolver

Coverage Status

A utility to simpify the repeated unique ID + promise dance.

import nextResolver from 'next-resolver';

const [next, resolve] = nextResolver();

// next unique identifier and its promise
const [id, promise] = next();

// pass the promise around, hold the id ...
// ... so that whenever it's done:
if (condition) {
  resolve(id, value);

  // or reject via
  resolve(id, null, new Error('reason'));
}

The default nextResolver() accepts an optional callback that will receive a unique identifier that can be used to return something else:

// use strings instead of numbers as IDs
const [next, resolve] = nextResolver(String);

const [id, promise] = next();
typeof id; // string

// use any Map key variant
const [next, resolve] = nextResolver(id => {
  // make it stronger (not really useful)
  return `${id}-${crypto.randomUUID()}`;
  // make it a unique ref
  return { id };
});

Please note that if the returned value is still awaited, the next id will be passed to create a new identifier (so don't put too much logic within the id creation, it's already granted to be unique per that logic/session).

This is mostly needed in case you want to brand your async operations, so that something like a prefix would be enough:

const [next, resolve] = nextResolver(id => `my-logic-${id}`);

And that's all folks 🥕

Keywords

promise

FAQs

Package last updated on 25 Apr 2025

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