tree-visitor-async

Visit nodes in the tree asynchronously and sequentially. Supports promises.

npm install tree-visitor-async
23 downloads in the last day
117 downloads in the last week
409 downloads in the last month

Tree Visitor Async

Visit nodes in the tree asynchronously and sequentially. Support promises.

Asynchronous version of Tree Visitor. Actions can return a promise and .visit() returns a promise.

API

var fs = require('fs');
var Q = require('q');
var VisitorAsync = require('tree-visitor-async');
var nodes = [
    { type: 'import', value: 'path/to/file1' },
    { type: 'import', value: 'path/to/file2' },
];

function MyVisitorAsync() {}
MyVisitorAsync.prototype = new VisitorAsync();

MyVisitorAsync.prototype.visit_import = function (importNode) {
    var deferred = Q.defer();
    fs.readFile(importNode.value, 'utf8', deferred.makeNodeResolver());
    return deferred.promise.then(function (content) {
        console.log(content);
    });
};

new MyVisitorAsync().visit(nodes).then(function () {
    console.log('done');
});

Methods are passed to the returned promise as fulfill callbacks. So, for example, if a method throws an error or returns a rejected promise, subsequent nodes won't be visited.

this keyword in the fulfill & reject callbacks refers the created visitor object (e.g., new MyVisitorAsync() in the previous example).

npm loves you