In Angular (and other JavaScript frameworks), data from an http request is retrieved async using a promise and can be set to a variable using a callback. Simple enough. But I'm getting tired of writing code that looks like this:
service.getWidgetsAsync(function (result) { vm.widgets = result.data });
Is there any way that I haven't thought of to write the above more like this....?
vm.widgets = service.getWidgetsAsync();
"No" is probably a valid answer. :-)
(Example of the getWidgetsAsync added to clarify: )
function getWidgetsAsync(callback) {
$http.get(APPBASEURL + 'Foo/GetWidgets')
.then(function(result) {
callback(result);
});
}
.then()method. You don't call them directly as functions. Could you name an actual API that you're using instead of a fake name like "getWidgetsAsync"? Then we can find out if it has anything to do with promises.