Skip to main content

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

Project description

next-resolver

Coverage Status

Social Media Photo by Jan Genge on Unsplash

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 🥕

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

next_resolver-0.0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

next_resolver-0.0.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file next_resolver-0.0.1.tar.gz.

File metadata

  • Download URL: next_resolver-0.0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for next_resolver-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2f89c46169986709fa9f35310d13fd19f92bf8e605edf0ba68497e0741abddb1
MD5 c96eb7f72ed7a40befe3ae0bcede55ce
BLAKE2b-256 2141fc4612a16af963d9f7e7d61d079a8100e466441250ac0c836f54526ec26b

See more details on using hashes here.

File details

Details for the file next_resolver-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: next_resolver-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for next_resolver-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 72c2d60600b0acf2fa450c43fc4a3e9c21ad6ba3700b15be60af29a7dd2cd502
MD5 c4179e7d975ea8dc4f96e217b42c38b5
BLAKE2b-256 14b246a05e0fab8e12b005089d1d89a5886702fad095a8e28412eee59f4c92f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page