[SOLVED] Return fetch api response from a function | SoloLearn: Learn to code for FREE!

+2

[SOLVED] Return fetch api response from a function

How can I return the response received from fetch() api using a function and assign a function to a variable and use it's result values. Example code: fetchData = () => { fetch('https://example.com/api', { method: 'POST', body: "some-data", }) .then(response => response.json()) .then(data => { result = JSON.stringify(data); console.log('Success:', result); return result; }) .catch((error) => { console.error('Error:', error); }); } // a = compile() console.log(a.Result)

10/19/2020 10:15:03 AM

March Zucherberg

12 Answers

New Answer

+2

https://code.sololearn.com/WPxj5iCZsBue/?ref=app Bibek Oli all i did was put the fetch inside "new Promise((resolve, reject) => fetch(....))" and inside .then(response) of fetch() i returned resolve(response). inside .catch(err) i returned reject(err) now i can compile() and tack on .then() to it which receives response as a parameter. and yeah, i put the first line "let usercode = ...." inside the onclick function, coz we have to get the newer value onclick, not the old value when it was initialized.

+4

You would have to make a promise, that takes resolve and reject as parameters. const fetchData = () => { return new Promise((resolve, reject) => ( fetch("https://jsonplaceholder.typicode.com/posts") .then(res => res.json()) .then(data => resolve(data)) .catch(err => reject(err)) )) } async function getData() { const data = await fetchData() console.log(data); } getData()

+3

you would have to make an async function so that u can use "await" keyword if you dont wanna make an async function u can use .then() fetchData().then(data => { // ... })

+3

u mean this? : fetchData().then(data => ()) data is received as a parameter because we are passing data from above, resolve(data) and if there is an error, inside catch block, we will do reject(err) so we can receive these arguments in .then and .catch respectively.

+3

Bibek Oli well i dont know the answer to that question coz i havent tried doing that object syntax with dom elements, but there's a mistake in ur code in that question: let alertBox = { //properties have colons (:) not (=) header: document.getElementById() }

+2

maf Thank you for your help 😍😍😍 The final code is easier than I expected. Thank you soon much.🙏🥰

+2

Bibek Oli glad i could help :) if u still dont understand, ask again.

+2

maf I understood it☺️☺️☺️ Thank you.🥰 If you have time, can you look at my newer question also, I didn't got any response on that.😁

+1

maf how is the data sent in this method, as a second parameter of fetch or??

+1

maf not that, in getting confused in this, can you apply this in my real code? It is a very simple code, just focused on above topic only. So that I can understand it better.🙏

+1

This is my code, it's very simple code; https://code.sololearn.com/WikTK27SFG0N/?ref=app

+1

maf ohh😂😂 I don't notice that, I was just making a dummy template to show an example.😁