Skip to content

curvedmark/tree-visitor-async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Visit nodes in the tree asynchronously and sequentially

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published