npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solid-primitives/promise

v1.0.16

Published

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

Downloads

438

Readme

@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.