A utility to simpify the repeated unique ID + promise dance (asyncio).
Project description
next-resolver (Python)
A utility to simplify the repeated unique ID + async promise pattern, built on asyncio.
Install from PyPI:
pip install next-resolver
Basic usage
from next_resolver import next_resolver
next_fn, resolve = next_resolver()
# next unique identifier and its awaitable
uid, promise = next_fn()
# pass `promise` around, keep `uid` …
# … then when the work is done:
if condition:
resolve(uid, value)
# or reject:
if error:
resolve(uid, None, error=ValueError("reason"))
promise is a Promise (subclass of asyncio.Future). Await it like any other future:
result = await promise
Custom ID types
The factory accepts a callable typed (default: int) that turns a running counter into an ID. Use str for string IDs, or any hashable value suitable for use as a dict key:
next_fn, resolve = next_resolver(str)
uid, promise = next_fn()
assert isinstance(uid, str)
next_fn, resolve = next_resolver(lambda n: f"job-{n}")
uid, promise = next_fn()
If the callable can still collide with an existing ID, the implementation increments and tries again until the ID is unused—so keep ID creation cheap and deterministic per counter value.
Promise
Promise extends asyncio.Future and adds chaining similar to JavaScript promises:
then(on_fulfilled, on_rejected=None)— returns anotherPromisecatch(on_rejected)— rejection-only handler
Nested Promise values can be flattened with the async helper Promise.value(promise).
Requirements
Python 3.8+
See also
The repository README documents the JavaScript package of the same name.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file next_resolver-0.0.3.tar.gz.
File metadata
- Download URL: next_resolver-0.0.3.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04b47b2ceb65dc02061396f20a9036a5c4e1574b1672a006a9bcbed013de498c
|
|
| MD5 |
f0688065abea8084bfb80ea2ea78a8f4
|
|
| BLAKE2b-256 |
bc94fc4391080df147fa8ff3813a84689472f508086726cb0c9db813706f5bd4
|
File details
Details for the file next_resolver-0.0.3-py3-none-any.whl.
File metadata
- Download URL: next_resolver-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
104d25f388368b7108e97717e81506a8739e6ad69449319b44223b2066304432
|
|
| MD5 |
aa43e00b68423d7335992d8a10181114
|
|
| BLAKE2b-256 |
f5ce5dbf34300c5e123f2711277f84217913fc40abca1c141386a01538f88ab9
|