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

only-promise

v1.0.1

Published

创建一个单例模式Promise调用函数,多处地方在同一时间调用同一个Promise函数时共享同一个Promise实例

Downloads

3

Readme

only-promise

创建一个单例模式Promise调用函数,多处地方在同一时间调用同一个Promise函数时共享同一个Promise实例,支持Typescript

使用

npm 安装

npm install only-promise -S

使用方法

import { onlyPromise } from "only-promise";

const getTime = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Date.now());
    }, 1000);
  });
};

const onlyGetTime = onlyPromise(getTime);

onlyGetTime().then((res) => {
  console.log("res", res);
});

Example

test1

import { onlyPromise } from "only-promise";

const getTime = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Date.now());
    }, 1000);
  });
};
// 不设置缓存时间
const onlyGetTime = onlyPromise(getTime);

for (let i = 0; i < 3; i++) {
  onlyGetTime()
    .then((res) => {
      console.log("res", res);
    })
    .catch((err) => {
      console.log("err", err);
    });
}

setTimeout(() => {
  onlyGetTime().then((res) => {
    console.log("result", res);
  });
}, 3000);

// res 1708251128183
// res 1708251128183
// res 1708251128183
// result 1708251131201

test2

import { onlyPromise } from "only-promise";

const getTime = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Date.now());
    }, 1000);
  });
};
// 设置缓存时间5秒,单位毫秒
const onlyGetTime = onlyPromise(getTime, 5000);

for (let i = 0; i < 3; i++) {
  onlyGetTime()
    .then((res) => {
      console.log("res", res);
    })
    .catch((err) => {
      console.log("err", err);
    });
}

setTimeout(() => {
  onlyGetTime().then((res) => {
    console.log("result1", res);
  });
}, 3000);

setTimeout(() => {
  onlyGetTime().then((res) => {
    console.log("result2", res);
  });
}, 10000);

// res 1708251344268
// res 1708251344268
// res 1708251344268
// result1 1708251344268  在缓存时间内的结果保持不变
// result2 1708251350275  超过缓存时间的结果会才会变化

token 无感刷新,这里只做简单处理,实际场景会更复杂一些

import axios from "axios";
import { onlyPromise } from "only-promise";
import Cookies from "js-cookie";
import * as jwt from "jsonwebtoken";

const http = axios.create({});

const getAccessToken = (refreshToken) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("新的access token:" + Date.now());
    }, 1500);
  });
};
// 由于可能会同时发起多个请求,如果不使用该方式会导致多次刷新token
const singleGetAccessToken = onlyPromise(getAccessToken);

http.interceptors.request.use(async (config) => {
  const token = Cookies.get("accessToken");
  if (!token) return config;
  // 解析token内容
  const jwtData = jwt.decode(token, { complete: true });
  if (jwtData && jwtData.payload) {
    const tokenData = jwtData.payload;
    const curNowTime = Date.now() / 1000;
    // 判断token是否已过期,如果已过期就重新获取新token
    if (tokenData.exp < curNowTime) {
      const refreshToken = Cookies.get("refreshToken");
      const newToken = await singleGetAccessToken(refreshToken);
      Cookies.set("accessToken", newToken);
      config.headers["Authorization"] = newToken;
    }
  }
  return config;
});