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

p-scheduler

v0.1.0

Published

`p-scheduler` providing a controlled way to manage the execution order of promises. It leverages proxies to queue and manage promises, making it easier to handle asynchronous operations sequentially, especially asynchronous third-party dependencies.

Downloads

3

Readme

p-scheduler

p-scheduler providing a controlled way to manage the execution order of promises. It leverages proxies to queue and manage promises, making it easier to handle asynchronous operations sequentially, especially asynchronous third-party dependencies.

Installation

npm install p-scheduler

Usage

Importing and Initializing

First, import the p-scheduler class and create an instance of it. You can pass an optional boolean parameter to the constructor to enable or disable automatic promise resolution.

import PScheduler from 'p-scheduler';

const scheduler = new PScheduler(); // Auto mode enabled by default
const manualScheduler = new PScheduler(false); // Auto mode disabled

Adding Asynchronous Functions

To add a function to the scheduler, use the add method. This method returns a proxied version of the function that will be executed according to the scheduler's rules.

const proxiedFunction = scheduler.add(originalFunction);

Auto Mode

In auto mode, promises are resolved automatically in the order they were added. This is the default behavior.

const scheduler = new PScheduler();

const fn1 = scheduler.add(async () => {
  console.log('Function 1');
});

const fn2 = scheduler.add(async () => {
  console.log('Function 2');
});

fn1();
fn2();

In this example, "Function 1" will always be logged before "Function 2", regardless of the individual execution times of the functions.

Manual Mode

In manual mode, you have control over when the next promise in the queue is resolved by calling the next or nextAll methods.

const scheduler = new PScheduler(false);

const fn1 = scheduler.add(async () => {
  console.log('Function 1');
});

const fn2 = scheduler.add(async () => {
  console.log('Function 2');
});

fn1();
fn2();

// Manually resolve the next promise in the queue
scheduler.next(); // Logs: "Function 1"

// Resolve all remaining promises in the queue
scheduler.nextAll(); // Logs: "Function 2"

API

Constructor

constructor(auto?: boolean)
  • auto (optional): A boolean indicating whether promises should be resolved automatically. Defaults to true.

Methods

add

add(fn: Function): Function
  • fn: The function to be added to the scheduler.
  • Returns: A proxied version of the function.

next

next(): void

Resolves the next promise in the queue. Only available when auto is set to false.

nextAll

nextAll(): void

Resolves all remaining promises in the queue. Only available when auto is set to false.

Example

Here is a complete example demonstrating the usage of p-scheduler:

import PScheduler from 'p-scheduler';

const scheduler = new PScheduler();

const fn1 = scheduler.add(async () => {
  console.log('Function 1');
  return 'Result 1';
});

const fn2 = scheduler.add(async () => {
  console.log('Function 2');
  return 'Result 2';
});

fn1().then(result => console.log(result));
fn2().then(result => console.log(result));

Output:

Function 1
Result 1
Function 2
Result 2

In this example, "Function 1" and its result will always be logged before "Function 2" and its result, maintaining the order of execution.

Credits

Inspired by p-mutex