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 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-async-ewt

v1.0.18

Published

基于axios封装异步请求

Downloads

28

Readme

use-async-ewt

基于axios封装异步请求

1、安装

  yarn: yarn add use-async-ewt
  npm: npm install use-async-ewt --save

2、httpConfig、http使用

2.1 在项目入口处导入,并初始化

index.tsx:

import { httpConfig } from 'use-async-ewt'

// 初始化配置
httpConfig(apis, reqRequest, resConfig, resErrorConfig, currentEnvName, mockEnvName)

2.2 参数说明

apis:
{
  [key: string]: {
    [key: string]: {
      method: string,
      url: string,
      mockUrl?: string,
      type?: string
    }
  }
}
reqRequest: 请求拦截器回调
resConfig: 响应拦截回调
resErrorConfig: 响应错误回调
currentEnvName: string // 当前项目环境变量名称
mockEnvName: string // mock环境名称

2.3 示例

import { httpConfig } from 'use-async-ewt'
import apis from './apis.ts'

// 请求拦截器回调
const reqRequest = function (config) {
  ...
  ...
  return config
}

// 响应拦截回调
const resConfig = function (response) {
  ...
  ...
  return response
}

// 响应错误回调
const resErrorConfig = function(error) {
  ...
  ...
  return Promise.reject(error)
}

httpConfig(apis, reqRequest, resConfig, resErrorConfig, 'REACT_APP_ENV', 'mock')

apis.ts:

import demo1 from 'demo1.ts'
export default {
  demo1,
}

demo1.ts:

export default {
  getUserInfo: { // 获取用户信息
    method: 'get',
    url: `${url}/api/getUserInfo`,
    mockUrl: 'http://mockUrl.com'
  },
  ...
}

2.4 组件中使用

import { http } from 'use-async-ewt'

const getUserInfo = async () => {
  const { data } = await http.getUserInfo(params)
  ...
}

3. useAsync

3.1 使用说明

const {data, loading, error, excute} = useAsync(()=>Promise<any>, boolean)

3.2 参数说明

useAsync(返回promise的函数, 是否立即执行) // 默认false不立即执行
data: 请求获取的数据
loading: loading状态
error: 请求错误的error
excute: 调用该请求函数的方法

3.3 使用示例

import { useAsync, http } from 'use-async-ewt'

const { execute, data, loading} = useAsync(()=> http.getUserInfo(params), true)
if(loading) {
  return <Loading />
}
return <h1>{ data.useName }</h1>