JSFiddle - React, Tailwind, and code Playground
by klarstil
HTML
<button class="test-me">
Test me!
</button>
<div class="output">
</div>
JavaScript
function fetchPost(id) {
id = id || 1;
var jQueryPromise = $.ajax({
'url': 'https://jsonplaceholder.typicode.com/posts/' + id
});
return Promise.resolve(jQueryPromise);
}
var postIds = [ 2, 4, 7, 42 ];
$('.test-me').on('click.test-me', function() {
var sequencePromise = postIds.reduce(function(promise, item) {
return promise.then(function(results) {
return fetchPost(item)
.then(results.push.bind(results))
.then(function() { return results; });
});
}, Promise.resolve([]));
sequencePromise.then(function(results) {
alert(JSON.stringify(results));
});
});