Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now that we're successfully getting data asynchronously from the Open Notify API, let's use it to make another AJAX call, this time to the Wikipedia API.
Why return callback()
vs. only callback()
?
Both are perfectly acceptable. In fact, most of the time you might use:
if(xhr.status === 200) {
let data = JSON.parse(xhr.responseText);
callback(data);
}
instead of:
if(xhr.status === 200) {
let data = JSON.parse(xhr.responseText);
return callback(data);
}
In this case, it's using return
to ensure that execution in the current scope does not continue past invoking callback(data)
. This might prevent multiple callbacks from being accidentally invoked, for example. In any case, it provides a clear way of short-circuiting the function you're in.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up