npm.io
6.10.0 • Published 2d ago

@solana/promises

Licence
MIT
Version
6.10.0
Deps
0
Size
78 kB
Vulns
0
Weekly
1.4M

npm npm-downloads
code-style-prettier

@solana/promises

This package contains helpers for using JavaScript promises.

Functions

getAbortablePromise(promise, abortSignal?)

Returns a new promise that will reject if the abort signal fires before the original promise settles. Resolves or rejects with the value of the original promise otherwise.

const result = await getAbortablePromise(
    // Resolves or rejects when `fetch` settles.
    fetch('https://example.com/json').then(r => r.json()),
    // ...unless it takes longer than 5 seconds, after which the `AbortSignal` is triggered.
    AbortSignal.timeout(5000),
);
isAbortError(err)

Returns true if err is an Error whose name is 'AbortError'. Use this to distinguish abort rejections from other failures without having to instanceof-check every platform-specific error class.

try {
    await getAbortablePromise(doWork(), signal);
} catch (e) {
    if (isAbortError(e)) {
        // The operation was aborted; don't surface as an error.
        return;
    }
    throw e;
}
safeRace(...promises)

An implementation of Promise.race that causes all of the losing promises to settle. This allows them to be released and garbage collected, preventing memory leaks.

Read more here: https://github.com/nodejs/node/issues/17469

Keywords