Skip to content

Instantly share code, notes, and snippets.

@gartenfeld
Last active August 29, 2015 14:27
Show Gist options
  • Save gartenfeld/527e56a3c30537687257 to your computer and use it in GitHub Desktop.
Save gartenfeld/527e56a3c30537687257 to your computer and use it in GitHub Desktop.
Promises using Q.
var Q = require('q');
var async = function (prefix) {
// create a promise wrapper
var hold = Q.defer();
var success = function () {
var data = prefix + "SOLVED!";
// by calling 'resolve', the next steps in the
// 'then' handler (e.g. 'console.log' below)
// will be executed with 'data' passed in
hold.resolve(data);
};
// simulating an async call
setTimeout(success, 100);
// the promise object is returned immediately
return hold.promise;
};
async('RE')
.then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment