Created
December 11, 2017 22:18
-
-
Save t3db0t/dc91f00ea4b39c5478a7b841b2fe08c0 to your computer and use it in GitHub Desktop.
Trying to build an asynchronous recursive tree builder
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
| const Redis = require("ioredis"); | |
| const redisOpts = { | |
| port: 6379, | |
| host: "127.0.0.1" | |
| }; | |
| var _redis = new Redis(redisOpts); | |
| var blacklist = []; | |
| function buildNode(id) { | |
| console.log("buildNode id", id); | |
| // const url = await _redis.get(id+":url"); | |
| // const title = await _redis.get(id+":title"); | |
| // const childIDs = await _redis.smembers(id+":links"); | |
| blacklist.push(id); | |
| console.log("blacklist",blacklist); | |
| return _redis.pipeline().get(id+":url").get(id+":title").smembers(id+":links").exec().then(results => { | |
| var url = results[0][1], | |
| title = results[1][1], | |
| childIDs = results[2][1]; | |
| // console.log(results); | |
| console.log("starting children...", childIDs); | |
| var children = childIDs.map((cid) => { | |
| if(!blacklist.includes(cid)) buildNode(cid).then(result => { console.log(result); return result }); | |
| }); | |
| return { id, url, title, children }; | |
| }); | |
| } | |
| // read data from Redis and construct in-memory graph for renderer | |
| function main() { | |
| _redis.get("startID").then(startID => { | |
| // blacklist.push(startID); | |
| var sitemapTree = buildNode(startID).then(sitemapTree => { | |
| console.log("done",JSON.stringify(sitemapTree)); | |
| }); | |
| }); | |
| }; | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment