javascript - Node.js - How do you control Async callback sequence? -


in following example walking on directory tree obtain listing of directories , files. importantly need order of directories , files same visually @ them in file explorer. brevity writing out folders , files console.log in sequence expect them...

var fs = require('fs'); var path = require('path');  var walk = function(dir) {     console.log(dir);     var files = [];     var items =  fs.readdirsync(dir).sort(function (a, b) {         return a.tolowercase().localecompare(b.tolowercase());     });     items.foreach(function(item){         if (item.charat(0) !== '.') {             var itempath = path.join(dir,item);             if (fs.lstatsync(itempath).isdirectory()) {                 walk(itempath);             } else {                 files.push(itempath);             }         }     });     files.foreach(function(file){         console.log(file);     }); };  walk('c:\\foo\\bar'); 

this sync version took of 20 minutes develop , enforces exact sequence need.

turning async solution great example of how making node.js "scalable" can become verbose, complex , time consuming on other languages.

whilst interested in how others solve in pure way (enforcing sequence), interested in how other developers using boiler plate libraries type of problem.

there seems numerous solutions on npm have adopted own implementation. without taking time learn them all, not sure 1 pick.

the bottom line question... how turn above code async solution whilst enforcing callback sequence. examples helpful. if use boiler plate library, why did choose it. interested hear other people comments, in particular around style , readability of various solutions.

update 1

i need enforce callback sequence of async events happening within recursive function. not confused enforcing call sequence of nested callbacks can solved promises, e.g. then().then().then()...

what need happen recursive walk() function fire 1 @ time, such next walk() waits previous walk() complete. "effectively waits" idea of should happen, making code "really wait" imply pausing code not right solution.

although wonder if solution somehow implements process.nexttick() park next walk() 1 direction at.

why shouldn't using promises when comes async operations

simplified, customized version of bluebird example

var promise = require("bluebird"); var join = promise.join; var fs = promise.promisifyall(require("fs")); var path = require("path")  var walk = function(dir){     return fs.readdirasync(dir).map(function(filename) {         var file = path.join(dir, filename);         var stat = fs.statasync(file);         return join(stat, function(stat) {             return {                 stat: stat,                 filename: file             }         });     }).call("sort", function(a, b) {         return a.filename.localecompare(b.filename);     }).each(function(file) {         if(file.stat.isdirectory()){             walk(file.filename);         }         console.log(file.filename + " last modified " + file.stat.mtime)     }) }  walk('./'); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -