Skip to content

Instantly share code, notes, and snippets.

@omoshetech-t
Created September 1, 2015 20:43
Show Gist options
  • Save omoshetech-t/75783e15329fd5544537 to your computer and use it in GitHub Desktop.
Save omoshetech-t/75783e15329fd5544537 to your computer and use it in GitHub Desktop.
Example of avoiding Callback Hell with Ext JS.
Ext.Deferred.all([fetch('hello.json'), fetch('world.json')])
.then(function(responses) {
var hello = getMessage(responses[0]),
world = getMessage(responses[1]);
Ext.Msg.alert('Success', hello + world);
})
.otherwise(function(response) {
var error = getMessage(response);
Ext.Msg.alert('Failure', error);
});
function fetch(url) {
var deferred = new Ext.Deferred();
Ext.Ajax.request({
url: url,
success: function(response, opts) {
deferred.resolve(response);
},
failure: function(response, opts) {
deferred.reject(response);
}
});
return deferred.promise;
}
function getMessage(response) {
var json = response.responseText;
return Ext.JSON.decode(json).message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment