
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
when-traverse
Advanced tools
Asynchronously traverse tree of mixed promises and values
Consider this function as Promise.map + Promise.all for trees.
Often enough, we have tree structures, and when we want to transform them asynchronously, we have no other options but use sync variants of functions or write ugly hacks by collecting arrays of inner promises, handling them with Promise.all, collecting new promises (if there are), waiting for them with Promise.all again and do a lot of other silly stuff.
This function allows you to wait for tree of mixed promises and simple values, replace some nodes with asynchronous content, remove nodes depending on asynchronous conditions etc.
It handles each node as soon as it becomes available for processing, thus resulting in low overall latency.
Function assumes that DOM API compliant Promise exists in global namespace (true for latest versions of Chrome and Firefox).
If it's not, for AMD and Node.js polyfill will be loaded and used.
Use with AMD, Node.js or simple <script src>.
// delayed promise helper (for example only)
function delay(timeout, value) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(value);
}, timeout);
});
}
// sample tree with nested simple and promised nodes
var tree = {
a: 1,
b: delay(1000, {
'b1': delay(2000, 2),
'b2': delay(3000, 3)
}),
c: delay(4000, 4),
d: {
shouldNotGoHere: delay(5000, 5)
}
};
whenTraverse(tree, {
enter: function enter(node, key, parentNode) {
// is called when node object itself is resolved but didn't enter subtree yet
},
leave: function leave(node, key, parentNode) {
// is called when node with all the children are resolved and subtree is processed
}
}).then(function (tree) {
// got resolved tree here
});
or
// custom visit() function, you can process node and call this.into(node)
whenTraverse(tree, function visit(node, key, parentNode) {
// process each node here
return this.into(node); // and recursively visit children if needed
}).then(function (tree) {
// got resolved tree here
});
or
// no changes, only waiting for all the promises in tree
whenTraverse(tree).then(function (tree) {
// got resolved tree here
});
enter and leave you can return either:whenTraverse.SKIP to skip further processing of this node and children (useful in enter when you don't want to wait for children transformations nor get this node in leave);whenTraverse.REMOVE to remove this node in parent;Promise of anything listed above.Check out test.js for more code.
MIT © Ingvar Stepanyan
FAQs
Asynchronously traverse tree of mixed promises and values
The npm package when-traverse receives a total of 464 weekly downloads. As such, when-traverse popularity was classified as not popular.
We found that when-traverse 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.