Skip to content

Instantly share code, notes, and snippets.

@jcblw
Last active December 15, 2015 18:28
Show Gist options
  • Save jcblw/5303607 to your computer and use it in GitHub Desktop.
Save jcblw/5303607 to your computer and use it in GitHub Desktop.
how to handle errors with api constructor
// you dont need new but can have new
var api = apiConstructor();
var success = function(res){
console.log(res);
};
//passes error object eg. -> new Error("this is an error")
var error = function(err){
console.log(err.message);
};
api.addCommand('user/check_urlname').
addCall({
'url_name' : url_name
}).
sendData();
// error is the second parameter
api.complete(success, error);
// mainly handles could not recieve json
// also now you can stop api calls if user navigates away from data being loaded
// this is just a eg.
nav.onChange = function(){
// this is the actual call
api.cancel();
// then you can start another call
api.addCommand('user/check_urlname').
addCall({
'url_name' : url_name
}).sendData();
api.complete(success, error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment