Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Write fewer lines of code by turning node-style asynchronous functions and promises to a synchronous code using ES6 generators.
few is an npm module intended to run on node.js 4.0.0 and higher.
This package is continuously tested on all minor versions from node.js 4.0.0 and higher using Travis CI.
Using npm:
npm install few
Using bower:
bower install few
Supporting all modern browsers using Babel.
const few = require('few');
function returnValue(v, callback) {
process.nextTick(() => callback(null, v));
}
function* generateValue(v) {
return yield cb => returnValue(v, cb);
}
// Multiple invocations of few run asynchronously
few(function* () {
// Yield or delegate directly
const a = yield cb => returnValue(1, cb);
const b = yield Promise.resolve(2);
const c = yield 3;
const d = yield* generateValue(4); // yield generateValue(4); Also works
// Prints 1 2 3 4
console.log(a, b, c, d);
});
few(function* () {
// Parallelize using arrays
const arr = yield [
cb => returnValue(1, cb),
Promise.resolve(2),
3,
generateValue(4)
];
// Prints [ 1, 2, 3, 4 ]
console.log(arr);
});
few(function* () {
// Parallelize using objects
const obj = yield {
a: cb => returnValue(1, cb),
b: Promise.resolve(2),
c: 3,
d: generateValue(4)
};
// Prints { a: 1, b: 2, c: 3, d: 4 }
console.log(obj);
});
genOrFn
must be an initialized generator or a generator function that does not expect any arguments. Any other type will produce a TypeError
.
callback
, if provided, must be a node-style callback, i.e. accepting an error and a result as arguments.
The return value of the generator will be provided as the result argument and if an error is thrown, it will be provided as the error argument.
If callback
is not provided, any error that the generator produces, will be thrown.
few supports the following types to be yielded:
few allows parallelization by yielding an array or an object.
The yielded object or array may contain any combination of:
When all given elements have finished processing, a new object or array that contains the results of the given elements in the same order will be returned.
If any of the elements provides an error, the error will be thrown inside the generator.
Delegation is supported using the yield*
expression.
To run in parallel, a generator can be passed as an element of the yielded array.
Any error originating from yielded objects will be thrown inside the generator, and can be caught using try...catch
.
For example, the following code prints ERROR to stderr:
few(function* () {
try {
yield Promise.reject(new Error('ERROR'));
} catch (err) {
console.error(err.message);
}
});
If an error is thrown inside a generator (and not caught), it will be passed to the callback given as a second argument to few
or thrown if a callback has not been given.
The following example also prints ERROR to stderr:
few(function* () {
yield Promise.reject(new Error('ERROR'));
}, (err, result) => { console.error(err.message); });
In the following example, the uncaught error will crash the process. Make sure you handle all errors!
few(function* () {
yield Promise.reject(new Error('ERROR'));
});
Licensed under Apache 2.0
v1.2.0
yield [myGeneratorFunction, myGeneratorFunction()]; // Both works!
yield
:// All of the below works!
yield* myGeneratorFunction();
yield myGeneratorFunction;
yield myGeneratorFunction();
yield*
is still preferable since it performs a native delegation.
FAQs
Write asynchronous code in a synchronous fashion
The npm package few receives a total of 1 weekly downloads. As such, few popularity was classified as not popular.
We found that few demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.