@solid-primitives/promise
TypeScript icon, indicating that this package has built-in type declarations

1.0.17 • Public • Published

Solid Primitives Promise

@solid-primitives/promise

turborepo size size stage

A library of reactive primitives and helpers for handling promises.

  • promiseTimeout — Creates a promise that resolves (or rejects) after given time.
  • raceTimeout — Combination of Promise.race() and promiseTimeout.
  • until — Promised one-time watch for changes. Await a reactive condition.

Installation

npm install @solid-primitives/promise
# or
yarn add @solid-primitives/promise

promiseTimeout

Creates a promise that resolves (or rejects) after given time.

How to use it

import { promiseTimeout } from "@solid-primitives/promise";

await promiseTimeout(1000); // resolves after 1 second

try {
  await promiseTimeout(1000, true, "timeout"); // rejects with 'timeout' after 1 second
} catch (e) {
  console.log(e); // 'timeout'
}

raceTimeout

Combination of Promise.race() and promiseTimeout.

How to use it

import { raceTimeout } from "@solid-primitives/promise";

await raceTimeout(myPromise, 1000); // resolves after 1 second, or when "myPromise" resolves

try {
  await raceTimeout(myPromise, 1000, true, "timeout"); // rejects with 'timeout' after 1 second, or resolves when "myPromise" resolves
} catch (e) {
  console.log(e); // 'timeout'
}

until

Promised one-time watch for changes. Await a reactive condition.

How to use it

It takes a signal or a reactive condition — which will resolve the promise if truthy — as an argument.

Returns a promise that resolves a truthy value of a condition. Or rejects when it's root get's disposed.

With a custom reactive condition:

no need for createMemo, it's memoized internally

import { until } from "@solid-primitives/promise";

const [count, setCount] = createSignal(0);

await until(() => count() > 5);

With createResource

It also can be used as a source for createResource.

import { until } from "@solid-primitives/promise";

const [state, setState] = createSignal(null);

const [data] = createResource(() => until(state));

With raceTimeout

To limit the maximum time it has for resolving

import { until, raceTimeout } from "@solid-primitives/promise";

try {
  const result = await raceTimeout(until(condition), 2000, true, "until was too slow");
  // if until is quicker:
  result; // => truthy condition value
} catch (err) {
  // if timeouts:
  console.log(err); // => "until was too slow"
}

Manually stopping computation

If you don't want to use raceTimeout, there are other ways to stop the reactive computation of until if needed.

First, it will stop itself "onCleanup".

// the same goes for components as they are roots too
createRoot(dispose => {

  // disposing root causes the promise to reject,
  // so you need to catch that outcome to prevent errors
  until(condition)
    .then(res => {...})
    .catch(() => {})

  dispose()
})

Second, using the .dispose() method.

// until returns a promise with a dispose method in it
const promise = until(condition);

// you need to catch the rejection here too
promise.then().catch();

promise.dispose();

Demo

until + createResource demo: https://codesandbox.io/s/until-resource-demo-sfs7c?file=/src/index.tsx

until as createResource fetcher: https://codesandbox.io/s/until-as-resource-fetcher-6sl0e?file=/src/index.tsx

Changelog

See CHANGELOG.md

Inspiration

Original idea for this primitive comes from a VueUse's function of the same name.

Package Sidebar

Install

npm i @solid-primitives/promise

Weekly Downloads

1,132

Version

1.0.17

License

MIT

Unpacked Size

15.9 kB

Total Files

7

Last publish

Collaborators

  • thetarnav.
  • lexlohr
  • davedbase