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

race-control

v1.0.2

Published

一个用于处理异步操作竞态条件的 JavaScript 工具。它确保只有最新的操作会被执行,并且会取消任何先前的操作,帮助你更好地管理异步任务的执行顺序

Downloads

17

Readme

race-control

NPM version NPM downloads

race-control 是一个用于管理异步操作竞态条件的 JavaScript 工具。这个包帮助确保只有最新操作的结果会被应用,取消任何先前挂起的操作。

Install

$ npm install race-control

Usage

simple

import raceControl from "race-control";

const fetchData = (params, signal) => {
  return fetch("https://api.example.com/list", { params, signal });
};

const raceFetch = raceControl(fetchData, true);

raceFetch(params1).then(console.log);
raceFetch(params2).then(console.log);

In React

import raceControl from "race-control";

const fetchData = (params, signal) => {
  return fetch("https://api.example.com/list", { params, signal });
};

const raceFetch = raceControl(fetchData, true);

function SearchList() {
  const [list, setList] = useState([]);

  async function onInputHandle(e){
    raceFetch({ text: e.target.value }).then(data => setList(data))
  }

  return (
    <div>
      <input onInput={onInputHandle}/>
      <ul>
        {list.map((i) => (
          <li key={i.id}>{i}</li>
        ))}
      </ul>
    </div>
  );
}

Test Example

let delay = [1000, 2000, 6000, 4000, 5000];
const mock = (count, abortSignal) =>
  new Promise((resolve) => setTimeout(() => resolve(count), delay[count]));

// 使用前
mock(0).then(console.log);
mock(1).then(console.log);
mock(2).then(console.log);
mock(3).then(console.log);
mock(4).then(console.log);
// 输出 0 1 3 4 2

// 使用后
const raceMock = raceControl(mock);
raceMock(0).then(console.log);
raceMock(1).then(console.log);
raceMock(2).then(console.log);
raceMock(3).then(console.log);
raceMock(4).then(console.log);
// 输出 4

API

raceControl(target, abortSignal) 参数:

  • target (Function): 一个返回 Promise 的异步函数,
  • abortSignal (Boolean): 是否使用 AbortController 取消先前的操作。默认为 false。当该项为 true 时,target最后一个参数固定为 AbortController.signal 该项用于 fetchsignal 入参,详细请参考 UsageIn React 例子

return: 一个处理竞态条件的函数

LICENSE

MIT