Created
May 30, 2015 20:16
-
-
Save kishmiryan-karlen/b8f9435831053897729a to your computer and use it in GitHub Desktop.
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
// 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