Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
promise-accum
Advanced tools
npm install promise-accum
Often when using promises, variable scoping becomes an issue, specifically when doing an operation that requires something along the lines of "load two things in the database, then do something based on that".
There are a few ways to solve this problem:
Stay in callback hell
LoadItemOne().then(function(item1) {
return LoadItemTwo().then(function(item2) {
return DoSomethingWithItems(item1, item2);
});
});
Enter variable scoping hell
var item1;
LoadItemOne().then(function(i) {
item1 = i;
return LoadItemTwo();
}).then(function(item2) {
return DoSomethingWithItems(item1, item2);
});
Return ugly objects
LoadItemOne().then(function(item2) {
return LoadItemTwo().then(function(item2) {
return { item1: item1, item2: item2 }
});
}).then(function(items) {
return DoSomethingWithItems(items.item1, items.item2);
});
This library provides a solution. Context is stored in a variable called $
. To add the result of a promise to the context, call $('key')
. In this case:
var $ = require('promise-accum');
LoadItemOne().then($('item1')).then(function($) {
return LoadItemTwo().then($('item2'));
}).then(function($) {
return DoSomethingWithItems($.item1, $.item2);
});
Using this solution, the context object must be passed through the entire chain:
LoadItemOne().then($('item1')).then(function($) {
$.item1.foo = 'bar';
}).then(function($) {
// $ is not the context, obviously.
});
In most cases, it's as simple as returning $
in the promise. However, in some promise libraries (q and bluebird, among others), errors can be handled by calling .catch(errorHandler)
. This is effectively an alias for .then(null, errorHandler)
– of course, in this case, the context does not get passed through, so any functions after the error handler it.
To mitigate this, promise-accum provides $.passthrough
:
LoadItemOne().then($('item1')).then($.passthrough, errorHandler).then(function($) {
// $ is maintained.
});
FAQs
Solve variable scoping and/or callback hell in promises
We found that promise-accum 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 researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.