Created
January 27, 2018 11:18
-
-
Save Safiyya/a0a9f6bb6160b717ddab8a01fe32f372 to your computer and use it in GitHub Desktop.
Traversing a tree asynchronously with Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.