Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getData = async () => { //async keyword will ensure that the function returns a promise.
- try{
- const response = await fetch('https://api-to-call.com/endpoint');
- if (response.ok) {
- const jsonResponse = await response.json(); //Since .json() is an asynchronous method we have to await until the promise status is resolved. Then we store the value to know what data the JSON holds.
- return jsonResponse;
- }
- throw new Error('Request failed!');
- }catch(error) {
- console.log(error);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement