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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@winwangqi/fetcher

v1.0.0

Published

An elegant and easy-to-use JavaScript library for managing the request process.

Downloads

14

Readme

Fetcher

An elegant and easy-to-use JavaScript library for managing the request process.

中文文档

get started

Installation

npm install -save @winwangqi/fetcher

Usage

const fetcher = new Fetcher({
  request() {
    // return request promise
  },

  onSuccess(response) {
    // do something...
  },

  // other options
})

// return request promise
fetcher.fetch()

API

Options

| field | type | default | required | description | | ------------- | ------------------------------------------------------------------------------------------------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------- | | request | () => Promise | / | Y | Request method, the return value must be Promise | | concurrent | boolean | true | N | Whether to support concurrent. If not, there can be only one request in fetching state at the same time | | timeoutMs | number | 0 | N | Request timeout duration. Request timeout throws TimeoutException | | maxRetryTimes | number | 0 | N | Maximum number of retries when the request fails | | onPending | Function | / | N | onPending event handler, triggered when the request is started | | onSuccess | (responseAfterPipe) => void | / | N | onSuccess event handler, triggered when the request is successful | | onFailure | Function | / | N | onFailure event handler, triggered when the request fails | | onComplete | ({ ok: boolean, payload: responseAfterPipe }) => void | / | N | onComplete event handler, triggered when the request is completed (success or failure) | | onChange | ({ status: 'pending' / 'success' / 'failure', payload: responseAfterPipe / error / undefined }) => void | / | N | onChange event handler, triggered when the request status changes | | onTimeout | Function | / | N | onTimeout event handler, triggered by request timeout | | responsePipe | response => responseAfterPipe | / | N | Pipeline for formatting Response data |

Fetcher instance properties

| filed | type | default | description | | -------- | ------- | ------- | ------------------------------------- | | fetching | boolean | false | Is fetching | | fetched | boolean | false | Whether the request is completed | | data | boolean | null | Response value for successful request | | error | boolean | null | Request failed error |

fetcher.fetch return value

fetcher.fetch return value is promise

| field | type | description | | ------ | --------------------------- | -------------------------------------------- | | then | (responseAfterPipe) => void | Request completion callback function | | catch | (error) => void | Request failed callback function | | cancel | Function | Cancel request | | clear | Function | Clear the timeout countdown for this request |

Demo

React

class Example extends React.PureCompoennt {
  constructor(props) {
    super(props)

    const self = this

    this.setState(
      {
        fetcher: new Fetcher({
          request() {
            return new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve('resolved')
              }, 1000)
            })
          },

          timeoutMS: 500,

          onChange() {
            self.forceUpdate()
          },
        }),
      },
      () => {
        this.state.fetcher.fetch()
      }
    )
  }

  render() {
    const { fetcher } = this.state

    return fetcher.fetched ? (
      <div>{fetcher.data}</div>
    ) : fetcher.fetching ? (
      <div>loading...</div>
    ) : fetcher.error ? (
      <div>error: {fetcher.error}</div>
    ) : null
  }
}

Since the request status is updated inside the fetcher, forceUpdate needs to be called in the onChange event to notify react to re-render

Vue

<template>
  <div v-if="fetcher.fetched">{{ fetcher.data }}</div>
  <div v-else>
    <div v-if="fetcher.fetching">loading</div>
    <div v-else-if="fetcher.error">{{ fetcher.error }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fetcher: new Fetcher({
        request() {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve('resolved')
            }, 1000)
          })
        },

        timeoutMS: 500,
      }),
    }
  },

  mounted() {
    this.fetcher.fetch()
  },
}
</script>