Package Exports
- tree-visitor-async
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tree-visitor-async) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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).