/    Sign up×
Bounties /Pin to ProfileBookmark

How do you return a response from an async JavaScript call?

+ 1,000
Copy linkTweet thisAlerts:
Dec 05.2022
to post a answer
JavaScript

1 Replies

Copy linkTweet thisAlerts:
@JaySODec 05.2022 — To return a response from an async JavaScript call, you can use the return keyword within the callback function for the async call. Here's an example:


async function getData() {
const result = await fetch('https://example.com/data.json');
return result;
}


The getData() function makes an async call using the fetch() method to retrieve some data from a URL. The await keyword is used to pause the execution of the function until the fetch() call is complete. When the fetch() call is finished, the return statement is executed, which returns the result of the fetch() call to the caller of the getData() function.

You can then use the await keyword again to wait for the getData() function to finish and get the returned result:


async function main() {
const result = await getData();
// do something with the result
}


Alternatively, you can use a promise to return the result of an async call. Here's an example using the Promise object:


function getData() {
return new Promise((resolve, reject) => {
fetch('https://example.com/data.json')
.then((result) => resolve(result))
.catch((error) => reject(error));
});
}


The getData() function returns a new Promise object. The Promise constructor takes a callback function as an argument, which is called with resolve and reject functions. The fetch() method is used to make an async call, and the then() and catch() methods are used to handle the success and failure cases, respectively. If the fetch() call is successful, the resolve() function is called with the result of the fetch() call, which fulfills the promise. If the fetch() call fails, the reject() function is called with the error, which rejects the promise.

You can then use the await keyword or the .then() and .catch() methods to handle the returned promise:


async function main() {
try {
const result = await getData();
// do something with the result
} catch (error) {
// handle the error
}
}

// or using .then() and .catch()

getData()
.then((result) => {
// do something with the result
})
.catch((error) => {
// handle the error
});


Using a Promise to return the result of an async call is a more flexible approach, because it allows you to handle the success and failure cases separately, and it allows you to use the .then() and .catch() methods, as well as the await keyword, to handle the returned promise.
×

Success!

Help @hq spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...