New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

node-powertools

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-powertools - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

dist/test.js

34

dist/index.js

@@ -114,2 +114,4 @@ (function (root, factory) {

// https://github.com/sindresorhus/p-state/blob/main/index.js
// https://stackoverflow.com/questions/30564053/how-can-i-synchronously-determine-a-javascript-promises-state
// https://dev.to/xnimorz/101-series-promises-2-how-to-get-current-promise-status-and-build-your-own-promise-queue-18j8
Powertools.getPromiseState = function (promise) {

@@ -140,2 +142,34 @@ util = util || require('util');

Powertools.waitForPendingPromises = function (promises, options) {
return new Promise(function(resolve, reject) {
// Fix promises
promises = Powertools.arrayify(promises);
// Fix options
options = options || {};
options.max = typeof options.max === 'undefined' ? 10 : options.max;
options.timeout = typeof options.timeout === 'undefined' ? 60000 : options.timeout;
// Poll for pending promises
Powertools.poll(() => {
const pending = promises.filter((promise) => Powertools.getPromiseState(promise) === 'pending');
// Stop and wait
if (pending.length >= options.max) {
// console.log(`Waiting for ${pending.length} pending promises to finish...`);
return false;
}
// We can continue
return true;
}, { interval: 100, timeout: options.timeout })
.then(() => {
return resolve();
})
.catch((e) => {
return reject(new Error('Timed out waiting for pending promises to finish'));
})
});
}
Powertools.queue = function (options) {

@@ -142,0 +176,0 @@ return new FunctionQueue(options);

2

package.json
{
"name": "node-powertools",
"version": "1.3.0",
"version": "1.4.0",
"description": "Powerful assistive functions for Node and Browser environments.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -133,2 +133,20 @@ <p align="center">

### powertools.waitForPendingPromises(promises, options)
Wait for `options.max` promises to resolve before continuing. This is useful for when you have a large number of promises and you want to limit the number of concurrent promises that are running at any given time. This promise **rejects** if the `options.timeout` is reached.
This method depends on `util` from Node.js, so it will not work in the browser.
```js
const promises = [
powertools.wait(1000),
powertools.wait(2000),
powertools.wait(3000),
];
console.log('Starting processing', promises);
await powertools.waitForPendingPromises(promises, {max: 2, timeout: 2000});
console.log('Finished processing', promises);
```
### powertools.escape(str)

@@ -135,0 +153,0 @@ Add the escape character `\` before any character in `str` that needs to be escaped for a `RegExp`.

@@ -114,2 +114,4 @@ (function (root, factory) {

// https://github.com/sindresorhus/p-state/blob/main/index.js
// https://stackoverflow.com/questions/30564053/how-can-i-synchronously-determine-a-javascript-promises-state
// https://dev.to/xnimorz/101-series-promises-2-how-to-get-current-promise-status-and-build-your-own-promise-queue-18j8
Powertools.getPromiseState = function (promise) {

@@ -140,2 +142,34 @@ util = util || require('util');

Powertools.waitForPendingPromises = function (promises, options) {
return new Promise(function(resolve, reject) {
// Fix promises
promises = Powertools.arrayify(promises);
// Fix options
options = options || {};
options.max = typeof options.max === 'undefined' ? 10 : options.max;
options.timeout = typeof options.timeout === 'undefined' ? 60000 : options.timeout;
// Poll for pending promises
Powertools.poll(() => {
const pending = promises.filter((promise) => Powertools.getPromiseState(promise) === 'pending');
// Stop and wait
if (pending.length >= options.max) {
// console.log(`Waiting for ${pending.length} pending promises to finish...`);
return false;
}
// We can continue
return true;
}, { interval: 100, timeout: options.timeout })
.then(() => {
return resolve();
})
.catch((e) => {
return reject(new Error('Timed out waiting for pending promises to finish'));
})
});
}
Powertools.queue = function (options) {

@@ -142,0 +176,0 @@ return new FunctionQueue(options);

@@ -65,2 +65,24 @@ const package = require('../package.json');

describe('.waitForPendingPromises()', () => {
describe('promise', () => {
// This test should fail because it waits for two promises that never complete
it('promise (pending) => string (resolved) - should fail', () => {
// Create an array of promises that never resolve
const promises = [new Promise(() => {}), new Promise(() => {})];
// This should reject because the promises never resolve
return assert.rejects(powertools.waitForPendingPromises(promises, 2, 1000));
});
// This test should pass because the promises do complete
it('promise (pending) => string (resolved) - should pass', () => {
// Create an array of promises that resolve after 1 second
const promises = [new Promise(resolve => setTimeout(resolve, 1000)), new Promise(resolve => setTimeout(resolve, 1000))];
// This should not reject because the promises do resolve
return assert.doesNotReject(powertools.waitForPendingPromises(promises, 2));
});
});
});
describe('.getKeys()', () => {

@@ -67,0 +89,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc