Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created November 22, 2013 19:45
Show Gist options
  • Save cowboy/7605686 to your computer and use it in GitHub Desktop.
Save cowboy/7605686 to your computer and use it in GitHub Desktop.
node task promise wtf
var Q = require('q');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
function MyTask() {
EventEmitter.call(this);
this._deferred = Q.defer();
this.then = this._deferred.then.bind(this._deferred); // ???
}
util.extends(MyTask, EventEmitter);
MyTask.prototype.run = function() {
this.run = function() { return this; };
// Sample code
var self = this;
setTimeout(function() {
self._deferred.resolve(123);
}, 1000);
return this;
};
exports.MyTask = MyTask;
var mt = new MyTask();
mt.on("debug", console.debug);
mt.run({}, etc).then(something);
@cowboy
Copy link
Author

cowboy commented Nov 24, 2013

var Q = require("q");

exports.run = function(logger, config, files) {
  var deferred = Q.defer();

  setTimeout(function() {
    logger.info("not doing anything with " + files);
    deferred.resolve(true);
  }, 1000);

  return deferred.promise;
};

// Config
var logger = new Logger({verbose: true, arbitraryData: "mytask"}).pipe(process.stdout);
var config = {foo: true};
var files = [{src: ["foo.js"], dest: "bar.js"}];

// Runn
require("mytask").run(logger, config, files).then(something);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment