JSFiddle - React, Tailwind, and code Playground
by klarstil
HTML
<button class="test-me">
Test me
</button>
JavaScript
function fetchPost(id) {
id = id || 1;
return Promise.resolve($.ajax({
'url': 'https://jsonplaceholder.typicode.com/posts/' + id
}));
}
function fetchAuthor(id) {
id = id || 1;
return Promise.resolve($.ajax({
'url': 'https://jsonplaceholder.typicode.com/users/' + id
}));
}
$('.test-me').on('click.test-me', function() {
fetchPost(1).then(function(post) {
return Promise.all([ post, fetchAuthor(post.userId) ]);
}).then(function(results) {
var data = {
'post': results[0],
'user': results[1]
};
alert(JSON.stringify(data));
});
});