Last active
August 29, 2015 14:02
-
-
Save itsananderson/bd0217c386d51f4bc33e to your computer and use it in GitHub Desktop.
How to chain promises and such
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="/q.js"></script> | |
<script src="/lodash.js"></script> | |
<script> | |
function makePromise(val) { | |
var deferred = Q.defer(); | |
deferred.resolve(val); | |
return deferred.promise; | |
} | |
makePromise(5).then(function(val) { | |
return {num: makePromise(val), numSquared: makePromise(val*val)}; | |
}).then(function(promises) { | |
return Q.all(_.values(promises)).then(function(values) { | |
return _.zipObject(_.keys(promises), values); | |
}); | |
}).then(function(obj) { | |
console.log(obj); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment