Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The asyncbox npm package provides a set of utility functions to work with asynchronous code in JavaScript. It simplifies common asynchronous operations such as waiting for conditions, retrying operations, and handling promises.
waitForCondition
The `waitForCondition` function allows you to wait for a specific condition to be met. In this example, the condition is met after 2 seconds, and the function waits up to 5 seconds, checking every 500 milliseconds.
const { waitForCondition } = require('asyncbox');
async function example() {
let conditionMet = false;
setTimeout(() => { conditionMet = true; }, 2000);
await waitForCondition(() => conditionMet, { waitMs: 5000, intervalMs: 500 });
console.log('Condition met!');
}
example();
retry
The `retry` function allows you to retry an asynchronous operation a specified number of times. In this example, the operation fails twice before succeeding on the third attempt.
const { retry } = require('asyncbox');
async function example() {
let attempt = 0;
const result = await retry(3, async () => {
attempt++;
if (attempt < 3) throw new Error('Failed attempt');
return 'Success';
});
console.log(result); // 'Success'
}
example();
sleep
The `sleep` function pauses execution for a specified amount of time. In this example, the code waits for 2 seconds between logging 'Start' and 'End'.
const { sleep } = require('asyncbox');
async function example() {
console.log('Start');
await sleep(2000);
console.log('End');
}
example();
The `async` package provides a collection of functions for working with asynchronous JavaScript. It includes utilities for parallel and sequential execution, as well as flow control. Compared to asyncbox, it offers a broader range of utilities but may be more complex to use.
The `bluebird` package is a fully-featured Promise library that provides additional functionality over native Promises, such as cancellation, progress tracking, and more. While it doesn't focus on utilities like asyncbox, it enhances promise handling and chaining.
The `p-retry` package is a simple utility for retrying failed promises. It is similar to the retry functionality in asyncbox but is more focused and configurable, offering options like exponential backoff.
A collection of ES7 async/await utilities. Install via NPM:
npm install asyncbox
Then, behold!
An async/await version of setTimeout
import { sleep } from 'asyncbox';
async function myFn () {
// do some stuff
await sleep(1000); // wait one second
// do some other stuff
};
An async/await way of running a method until it doesn't throw an error
import { sleep, retry } from 'asyncbox';
async function flakeyFunction (val1, val2) {
if (val1 < 10) {
throw new Error("this is a flakey value");
}
await sleep(1000);
return val1 + val2;
}
async function myFn () {
let randVals = [Math.random() * 100, Math.random() * 100];
// run flakeyFunction up to 3 times until it succeeds.
// if it doesn't, we'll get the error thrown in this context
let randSum = await retry(3, flakeyFunction, ...randVals);
}
You can also use retryInterval
to add a sleep in between retries. This can be
useful if you want to throttle how fast we retry:
await retryInterval(3, 1500, expensiveFunction, ...args);
Export async functions (Promises) and import this with your ES5 code to use it with Node.
var asyncbox = require('asyncbox')
, sleep = asyncbox.sleep
, nodeify = asyncbox.nodeify;
nodeify(sleep(1000), function (err, timer) {
console.log(err); // null
console.log(timer); // timer obj
});
If you have a whole library you want to export nodeified versions of, it's pretty easy:
import { nodeifyAll } from 'asyncbox';
async function foo () { ... }
async function bar () { ... }
let cb = nodeifyAll({foo, bar});
export { foo, bar, cb };
Then in my ES5 script I can do:
var myLib = require('mylib').cb;
myLib.foo(function (err) { ... });
myLib.bar(function (err) { ... });
npm test
FAQs
A collection of small async/await utilities
The npm package asyncbox receives a total of 246,090 weekly downloads. As such, asyncbox popularity was classified as popular.
We found that asyncbox demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.