Skip to content

Instantly share code, notes, and snippets.

@zshell31
Created August 5, 2015 10:04
Show Gist options
  • Save zshell31/8e54316e61e5a86e0450 to your computer and use it in GitHub Desktop.
Save zshell31/8e54316e61e5a86e0450 to your computer and use it in GitHub Desktop.
var next = function(p, $scope) {
var create = function($scope, time) {
var time = time || 1000;
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("Promise: " + $scope.val);
$scope.val++;
resolve($scope);
}, time);
});
};
if ($scope) {
return create($scope);
}
return p.then(function ($scope) {
return create($scope);
});
};
var i = 0;
var $scope = {
val: i
};
var p = next(p, $scope);
for (i = 4; i > 0; i--) {
console.log("Iteration: " + i);
p = next(p);
}
/* Also I've found some related articles for further reading:
> http://12devs.co.uk/articles/promises-an-alternative-way-to-approach-asynchronous-javascript/
> http://www.dustindiaz.com/async-method-queues
> http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment