tree-visitor-async

1.0.0 • Public • Published

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).

Readme

Keywords

none

Package Sidebar

Install

npm i tree-visitor-async

Weekly Downloads

5

Version

1.0.0

License

MIT

Last publish

Collaborators

  • curvedmark