node.js - Recursive promises in nodejs using Q.defer -


i've been trying figure out how manage recursive loop don't know size of parent tree, possible have infinite childs , each child having own.

what want make list of pages in our confluence, , find structure of confluence pages tree, have call api many times (every time per child) know content.

so, problem tried use promises, somehow everytime returns result @ first loop, , want return promises when there aren't more childs inside tree.

there method calling recursive function:

//this id of parent tree (the main page of confluence) substractpages('28432839489') .then(function(resultsubstract) {                       console.log("success");    console.log(resultsubstract); <-- undefined }) .fail(function(resultsubstract) {    console.log("error");    console.log(resultsubstract); }); 

this recursive function call many times childs found:

var pagetemp; var pagestemp; pagestemp = new pages(); function substractpages(pageid) {     var deferred = q.defer();     request({         url: 'https://confluence.com/wiki/rest/api/content/' + pageid + '/child/page?limit=1000',         qs: {},         method: 'get',         headers: {             'authorization' : 'basic token'         }     }, (err, response, body) =>{         if(!err && response.statuscode == 200)         {             var pages = json.parse(body);             pagetemp = new page();              if(pages.results.length > 0)             {                    //this call next funcion because element has childs                 deferred.resolve(requestpagelist(pages.results));             }             else             {                 deferred.resolve(false);             }         } else {             deferred.reject(err);         }     });     return deferred.promise; } 

here recursion happens:

function requestpagelist(results) {     async.eachlimit(results, 1, (page, cb) =>{         pagetemp = new page();         pagetemp.id = page.id;         pagetemp.title = page.title;         pagestemp.list.push(pagetemp);          //here call recursive method substract data:         substractpages(page.id)         .then(function()         {             cb();         })         .fail(function(err)         {             cb(err);         });          //      }, (err) => {         console.log("test");         if(err){             return "error";         }         else         {             return pagestemp;         }     }); } 


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -