Skip to content

Instantly share code, notes, and snippets.

@kara-ryli
Created April 14, 2013 19:58
Show Gist options
  • Save kara-ryli/5383976 to your computer and use it in GitHub Desktop.
Save kara-ryli/5383976 to your computer and use it in GitHub Desktop.
Boilerplate for a grunt multi task that leverages YUI's Promises module to sanely handle asynchronous tasks that atomically process multiple files.
/*jshint node:true*/
module.exports = function(grunt) {
var Y = require("yui/promise"),
name = "...",
desc = "...",
defaultOptions = { /* */ };
function executeAsyncTask(resolve, reject, file, options) {
// this is where your business logic goes
// call `resolve` when the task is finished
// call `reject(error[, errorCode])` if something goes wrong.
}
// return a function that returns an array of promises, one for
// each file passe to the multi task.
function getPromises(options) {
return function (file) {
return new Y.Promise(function (resolve, reject) {
executeAsyncTask(resolve, reject, file, options);
});
};
}
grunt.registerMultiTask(name, desc, function () {
var options = this.options(defaultOptions),
iterator = getPromises(options),
promises = this.files.map(iterator);
Y.batch.apply(Y, promises).then(this.async(), grunt.warn);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment