Created
September 1, 2015 20:44
-
-
Save omoshetech-t/c04c487ec2284f9062b6 to your computer and use it in GitHub Desktop.
Example of avoiding sequential 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
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