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

dena

v1.0.3

Published

Async Javascript Scheduler

Downloads

2

Readme

Dena

Dena is a dead simple scheduler to queue and run async javascript functions.

It turns your async functions into async functions that schedule themselves to use limited resources, so at any given time, only limited number of async tasks are being executed.

Example

Assume you are downloading some URLs, but you need a token to access them. You have limited number of tokens and you can only download one page at a time with one token.

let simulatedDownload = function (delay) {
  return new Promise((acc, rej) => setTimeout(acc, delay))
}

let download = async function (token, path, delay) {
  console.log(`[ ] Started downloading ${path}?token=${token}`);
  
  await simulatedDownload(delay);

  console.log(`[x] Finished downloading ${path}?token=${token}`);
}

You can use Dena, to convert your download function into a smarter download. Dena dynamically assigns a token to your function from a pool of tokens and call your function. Dena assumes the first argument of your function is always the configuration object that needs to be selected from the pool.

let simulatedDownload = function (delay) {
  return new Promise((acc, rej) => setTimeout(acc, delay))
}

let download = async function (token, path, delay) {
  console.log(`[ ] Started downloading ${path}?token=${token}`);
  
  await simulatedDownload(delay);

  console.log(`[x] Finished downloading ${path}?token=${token}`);
}


let tokens = ['1_1a2b3c', '2_4d5e6f', '3_7g8h9i'];
let denaDownload = Dena(tokens, download);

// notice how the first argument is not needed.
// Dena assings a free token to your function dynamically
denaDownload('/one', 1000);
denaDownload('/two', 500);
denaDownload('/three', 2000);
denaDownload('/four', 500); // this waits until /two is downloaded
denaDownload('/five', 100); // this waits until /one is downloaded

/*
  [ ] Started downloading /one?token=1_1a2b3c
  [ ] Started downloading /two?token=2_4d5e6f
  [ ] Started downloading /three?token=3_7g8h9i
  [x] Finished downloading /two?token=2_4d5e6f
  [ ] Started downloading /four?token=2_4d5e6f
  [x] Finished downloading /one?token=1_1a2b3c
  [ ] Started downloading /five?token=1_1a2b3c
  [x] Finished downloading /four?token=2_4d5e6f
  [x] Finished downloading /five?token=1_1a2b3c
  [x] Finished downloading /three?token=3_7g8h9i
*/

Also, each denaDownload return a Promise which gets resolved to download's return value once that instance is executed.

API

Dena(configurationPool, fn);

configurationPool: Array of objects, each element will be fed into fn as its first argument.

fn: async function. fn has to return a Promise, so Dena knows when the execution is done.

Return Value: async fn: Async function that executes fn with dynamically assigned configuration from configurationPool.