Skip to content

Instantly share code, notes, and snippets.

@timmfin
Last active December 1, 2015 15:02
Show Gist options
  • Save timmfin/e71fd945be449fb03257 to your computer and use it in GitHub Desktop.
Save timmfin/e71fd945be449fb03257 to your computer and use it in GitHub Desktop.
Silly example of using the readTree promise in the previous broccoli API
function MyPlugin(inputNodes, options) {
this.options = options;
// In this example, let's say we need to do something special with the first
// inputNode as compared to the others.
[this.firstNode, ...this.otherNodes] = inputNodes;
}
MyPlugin.prototype.read = function(readTree) {
let outputPaths = [];
this.options.startCallback(); // first "hook" from plugin options
readTree(this.firstNode).then(firstNodeOutput => {
outputPaths.push(firstNodeOutput);
this.options.secondCallback(); // another "hook" from plugin options
return promiseMapSeries(this.otherNodes, (n) => {
return readTree(n).then(out => {
this.options.postEachOtherCallback(); // yet another "hook" from plugin options
return out;
})
});
}).then(([...otherNodesOutput]) => {
outputPaths = outputPaths.concat(otherNodesOutput);
this.options.finalCallback(); // a finished "hook" from plugin options
return whateverWeWantToDoWith(outputPaths);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment