Skip to content

Instantly share code, notes, and snippets.

@omoshetech-t
Created September 1, 2015 20:44
Show Gist options
  • Save omoshetech-t/c04c487ec2284f9062b6 to your computer and use it in GitHub Desktop.
Save omoshetech-t/c04c487ec2284f9062b6 to your computer and use it in GitHub Desktop.
Example of avoiding sequential Callback Hell with Ext JS.
var hello, world;
fetch('hello.json')
.then(function(response) {
hello = getMessage(response);
return fetch('world.json');
})
.then(function(response) {
world = getMessage(response);
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