Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
p-scheduler
Advanced tools
`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.
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.
npm install p-scheduler
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
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);
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.
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"
constructor(auto?: boolean)
auto
(optional): A boolean indicating whether promises should be resolved automatically. Defaults to true
.add
add(fn: Function): Function
fn
: The function to be added to the scheduler.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
.
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.
Inspired by p-mutex
FAQs
`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.
We found that p-scheduler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.