Skip to content

Instantly share code, notes, and snippets.

@niisar
Created November 16, 2016 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niisar/62949c770bef4f42c4dd0663cd173a58 to your computer and use it in GitHub Desktop.
Save niisar/62949c770bef4f42c4dd0663cd173a58 to your computer and use it in GitHub Desktop.
Make a sync call using q.js
// http://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.6/q.js
function doSomethingAsync(item) {
return Q.promise(function(resolve) {
$("#results").append("<div>" + item + "</div>");
setTimeout(resolve.bind(null, item), 3000);
});
}
function doAsyncSeries(arr) {
return arr.reduce(function (promise, item) {
return promise.then(function(result) {
return doSomethingAsync(result+item);
});
}, Q(''));
}
var items = ['x', 'y', 'z'];
doAsyncSeries(items).then(function() {
$("#results").append("<div>complete</div>");
});
<div id="results"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment