Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
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
The npm package promise-accum receives a total of 2 weekly downloads. As such, promise-accum popularity was classified as not popular.
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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.