Last active
August 29, 2015 14:27
-
-
Save gartenfeld/527e56a3c30537687257 to your computer and use it in GitHub Desktop.
Promises using Q.
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
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