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

@refetty/async

v1.0.0-rc.11

Published

Refetty promise tools to manages promise state and handle promise aborts

Downloads

13

Readme

Refetty - Async

Refetty promise tools to manages promise state and handle promise aborts

Usage

To install run yarn add @refetty/async

Promise state manager

The execState is a simple handler, you will pass a promise to them was first params, and a boolean was second, if true, it will not execute promise initially, and wait for a manual call.

You will receive a array return with state (arr[0]) and a dispatch function (arr[1]) to re-execute promise when you want. State is a obersvable, so you can call subsbribe and unsubscribe methods.

import { execState } from '@refetty/async'

const lazy = true // default false

// to create state
const [state, run] = execState(promise, lazy)

// to listen state chages
const listner = state.subscribe(value => console.log('State changes:' value))

// to unsubscribe listner
state.unsubscribe(listner)

State value is an object with follow props:

  • status (string) - indicates the executes status, and can be one fo these: idle | pending | fulfilled | rejected | aborted
  • loading (boolean) - shorthand to check if a promise is in pending status
  • result - a resolved promise response with fulfilled status (cleared only on next execution error or success)
  • error - a rejected promise response with rejected or aborted status (cleared in all re-executions)

Note: aborted status depends on the promise to have an isAborted method that checks error (promise.isAborted)

Promise abort handler

In makeAbortable, we require two args: a function and an instanciable abort controller spec compilant.

This return an array with two items, the first is a handled dispatcher, every call will create a new instance of abort controller. And the aborter function as second. Let's me show the code:

const handler = params => signal => fetch({
	...params,
	signal
})

const [dispath, abort] = makeCancelable(handler, AbortController)

dispath({
	method: 'get',
	url: '/users
})

abort()

Note: the abort() function will not called automatically on call dispatch again.

Putting all together

To make our dev process painless, you have the option to use all-in-one method. The control method uses execState and makeCancelable under the hood. It's expect only one required param: promise. Abort Controller need be a prop on promise, like this:

import { control } from '@refetty/async'

const promise = params => signal => fetch(...)
promise.AbortContoller = AbortController

const [state, dispatch] = control(promise)

Unlike from makeCancelable, every dispatch call will call abort function automatically before. And if you need use abort by yourself, you can acess them on dispatch.abort()

It's has a second param too, option object when you can pass lazy and 'abortMessage`

const [state, dispatch] = control(promise, { lazy: true, abortMessage: 'This request was aborted!' })