javascript - Why use .success() with getJSON? -


for jquery function, reason use .success()? initial function execute upon success right? aren't executing twice when include .success()?

$.getjson( "ajax/test.json", function( data ) { //this execute upon success }) .success(function() {    //doesn't above doing?  }) 

if looking in isolation , happy scenario, yes: same thing. callback final argument of $.getjson gets called regardless of result of request.

the .success() way of doing inspired promises. can read more them here: https://promisesaplus.com

promises set better interface composing many asynchronous operations handling non-successfull scenarios.

you should use .then() instead of .success(): https://api.jquery.com/deferred.then/


Comments