Created
November 16, 2016 09:41
-
-
Save niisar/62949c770bef4f42c4dd0663cd173a58 to your computer and use it in GitHub Desktop.
Make a sync call using q.js
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
// 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>"); | |
}); |
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
<div id="results"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment