Created
April 14, 2013 19:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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