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

@anfo/promisequeue

v1.1.8

Published

a queue of promise

Downloads

24

Readme

@anfo/promisequeue

I am going to use my poor English to demostrate the simple usage of this lib. .. ok fine I m giving up

我是怎么想起来要弄个这个呢

How did I think about build this?

因为有一个函数内部有异步调用,而函数内引用了全局变量或不可重入,这个时候我希望它保持互斥,也就是函数本身有一个小队列,在被调用之后就会进入等待直到完成下次执行才会开始,当然这个函数可以在任何时候被调用。

Because there is an asynchronous call inside a function, and the global variable or non-reentrant is referenced in the function, at this time I want it to remain mutually exclusive, that is, the function itself has a small queue, and it will wait until it is completed after being called. The second execution will start, of course, this function can be called at any time.

quick start

npm install @anfo/promisequeue
import { queueUp } from '@anfo/promsiequeue' 

example

queueUp(function: Function, timeout: Number)

参数的函数会保持原来的this指针和参数传递

The parameter function will keep the original this pointer and parameter passing

tips:

  • 传入箭头函数,this不可改变,会保持箭头函数的this(Pass in the arrow function, this cannot be changed, it will keep this of the arrow function)
  • 传入普通函数,则this为此处原来的this(Incoming ordinary functions, this is the original this here)
const {queueUp} = require('@anfo/promisequeue')

let a = queueUp(async()=>new Promise(r=>setTimeout(() => {
    console.log('hello')
    r()
}, 1000)))

a()
a()
a()

queueUpAll(functions: Array, timeout: Number)

a function can be like:

async()=>{} or [async()=>{}, 1000/*timeout*/]
const { queueUpAll } = require('@anfo/promiseQueue')

let funcs = queueUpAll([
    async()=>new Promise(r=>setTimeout(()=>{console.log('a done');r()}, 1000)),
    async()=>new Promise(r=>setTimeout(()=>{console.log('b done');r()}, 1000))
])

funcs[0]()
funcs[0]()
funcs[1]()
funcs[0]()

PromiseQueue.prototype.finally(promiseFunc: Function that return a Promise, timeout: Number)

const { PromiseQueue } = require('@anfo/promisequeue')

let queue = new PromiseQueue

queue.finally(async ()=>new Promise(r=>{
    setTimeout(() => {
        console.log('done')
        r()
    }, 1000)
})).finally(async ()=>new Promise(r=>{
    setTimeout(() => {
        console.log('done')
        r()
    }, 1000)
})).finally(async ()=>new Promise(r=>{
    setTimeout(() => {
        console.log('done')
        r()
    }, 1000)
}))

delayFunc(func: Function, timeout: Number)

返回一个延迟调用的函数,例如输入的回调函数,在timeout时间内的连续调用,只有最后一次的调用会生效

Return a delayed call function, such as input callback function, continuous call within timeout time, only the last call will take effect

const { delayFunc } = require('../dist/index')

const delayCall = delayFunc(()=>{console.log('search')}, 1000)

delayCall()
delayCall()
delayCall()
delayCall()
delayCall()

throttle(func: Function, timeout: Number)

节流函数