Skip to content

Instantly share code, notes, and snippets.

@Safiyya
Created January 27, 2018 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Safiyya/a0a9f6bb6160b717ddab8a01fe32f372 to your computer and use it in GitHub Desktop.
Save Safiyya/a0a9f6bb6160b717ddab8a01fe32f372 to your computer and use it in GitHub Desktop.
Traversing a tree asynchronously with Promise
traversePromise(callback: ((n: Node) => Promise<void>)): Array<Promise<void>> {
let queue: Array<Promise<void>> = [];
let recursion = (node: Node) => {
if (node.children) {
node.children.forEach(function (child: Node) {
queue.push(callback.apply(this, [child]));
recursion(child)
});
}
}
recursion(this);
return queue;
}
@Safiyya
Copy link
Author

Safiyya commented Apr 3, 2019

This code sample is used to perform async operations on a tree structure. For instance, call a REST API to retrieve profile picture for each one of the employee profile in a organization chart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment