javascript - Callback after end of asynchronous recursive function -


the function below prints chrome bookmarks in folder recursively. how alter below function call function after final recursive loop processed? chrome.bookmarks.getchildren() asynchronous makes difficult know when function done processing everything.

thanks.

    (var = 0; < foldersarray.length; i++) {         // loop makes several calls different folder ids.         printbookmarks(foldersarray[i]);       }      // i'd code here run after above has      //finished processing          function printbookmarks(id) {         chrome.bookmarks.getchildren(id, function(children) {            children.foreach(function(bookmark) {                 console.debug(bookmark.title);                printbookmarks(bookmark.id);            });         });     } 

edit: sorry, don't think clear in initial code example. i've updated code show problem i'm having asynchronous function calling function multiple times. i'd code after printbookmarks function calls wait printbookmarks functions finish processing.

your asynchronous method instances may executing @ once, , don't know how many there beforehand. so, you'll have keep count , use callback when last asynchronous method done.

for (var = 0; < foldersarray.length; i++) {     // loop makes several calls different folder ids.     printbookmarks(foldersarray[i], thingstodoafter); }  function thingstodoafter() {     // i'd code here run after above has      // finished processing. }  var count = 0;  function printbookmarks(id, callback) {     count++;     chrome.bookmarks.getchildren(id, function(children) {         children.foreach(function(bookmark) {              console.debug(bookmark.title);             printbookmarks(bookmark.id, callback);         });         count--;         if (count === 0 && callback)             callback();     }); } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -