3 ms·
I think what you are looking for is done, to some extent, in angular.js with their resource library. The future object is returned from the resource and can be
by thecolorblue 13y ago
I think what you are looking for is done, to some extent, in angular.js with their resource library. The future object is returned from the resource and can be put directly into a template, which then re-renders when the response comes from the server. The problem I have found is that it does not clean up your javascript code as much as you would think. For example, error handling is still complicated.
I have had good luck using javascript promise objects to clean up complicated async calls.
for example, making multiple calls and waiting for all responses looks something like this:
promise = YourFavoriteAsyncLib.all([new Promise('/restaurants'), new Promise('/shops')])
promise.then(function(res) {
// res[0] === restaurants
// res[1] === shops
});
...or we can wait for one to finish and then make a couple more calls...
promise = new Promise('/restaurant/1');
promise.then(function(r) {
return YourFavoriteLibAgain.all([
new Promise('/restaurant/' + r.id + '/patrons'),
new Promise('/restaurant/' + r.id + '/menu')
]);
})
.then(function(res) {
// do more fun things synchronously
});
The details change, and some libraries act a little differently, but once you break they async calls into steps it becomes easy to read.
I think the point of all of these responses is that async cannot look like regular statements because they do not act like regular statements.