Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishmiryan-karlen/b8f9435831053897729a to your computer and use it in GitHub Desktop.
Save kishmiryan-karlen/b8f9435831053897729a to your computer and use it in GitHub Desktop.
// jQuery Ajax simulator
var $ = {
ajax: function(opts, returnData, throwError) {
console.log(`Request URL: ${opts.url}`);
console.log(`Request data: ${opts.data}`);
var timer = Math.round() * 10 + 1;
if (throwError && typeof opts.error === 'function') {
setTimeout(function() {
opts.error({
errorMessage: 'AJAX Request Error!'
});
}, timer);
return;
}
if (typeof opts.success === 'function') {
setTimeout(function() {
opts.success(returnData);
}, timer);
}
}
};
// Usage scenario with nested AJAX calls
$.ajax({
url: 'http://example.com/auth',
method: 'GET',
success: function(data) {
console.log('Response data: ' + JSON.stringify(data));
$.ajax({
url: 'http://example.com/profile',
method: 'POST',
data: data.id,
success: function(data) {
console.log('User info: ' + JSON.stringify(data));
},
error: function(err) {
console.log(console.log(err));
}
}, {
name: 'Karlen'
});
},
error: function(err) {
console.log(err);
}
}, {
id: 24
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment