Created
September 1, 2015 20:43
-
-
Save omoshetech-t/75783e15329fd5544537 to your computer and use it in GitHub Desktop.
Example of avoiding Callback Hell with Ext JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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