This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I have a server-side function that outputs a value from a database to /.netlify/functions/todos-read
I need to read it on another page, however the read() function returns undefined instead.
Here is what i tried:
function read() {
fetch('/.netlify/functions/todos-read').then(res => res.json()).then((out) => {
return out
}).catch(err => console.error(err));
}
Here is what i expected it to return:
{"ref":{"#ref":{"id":"236323245287014920","class":{"#ref":{"id":"nappi","class":{"#ref":{"id":"classes"}}}}}},"ts":1561634259400000,"data":{"value":1}}
Your read() function should return a promise, and callers should expect one...
function read() {
return fetch('/.netlify/functions/todos-read').then(res => res.json());
}
// call it
read().then(res => {
console.log(res);
}).catch(error => {
console.log(error);
});
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I am trying to get data from this API using fetch, and I know a promise pending is returned, but it isn't resolving itself so I can't use the data. I don't know what I am doing wrong.
function api () {
return fetch('https://api.punkapi.com/v2/beers').then(respo => { respo.json() } ).then(data => {
const beersData = data;
return beersData;
}).catch(error => {
console.error(error)
}) ;
}
api();
console.log(beersData)
First of all, you need to remove curly bracket in first then.
respo => respo.json() is equivalent to respo => { return respo.json() }.
And second, you need to handle promise when you call api function since api() also returns Promise.
function api () {
return fetch('https://api.punkapi.com/v2/beers')
.then(respo => respo.json())
.then(data => {
const beersData = data;
return beersData;
}).catch(error => {
console.error(error)
}) ;
}
api()
.then(res => {
console.log(res);
})
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I can't work out how to return the nested .then methods, and resolve the value to the function that called it?
I can print it, at the deepest level of the last .then(), but I would like to print the returned value in the event listener.
connectedCallback () {
this.input.addEventListener('input', event => {
this.search(this.input.value)
})
}
search (str) {
let searchResult = window.fetch(`http://localhost:3000/api/?q=${str}`)
.then(result => {
return result.json()
.then(result => {
return result
})
})
}
}
Use async/await to await the promise result in the event listener. You can also simplify your promise chain considerably:
connectedCallback() {
this.input.addEventListener("input", async event => {
const result = await this.search(this.input.value);
console.log(result);
});
},
search(str) {
return window
.fetch(`http://localhost:3000/api/?q=${str}`)
.then(result => result.json());
}
I would recommend reading up on promises and getting familiar with how they work.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I make an api call , check the data to make sure it's there but for some reason my array that it return is empty.
export const fetchFiveDayWeather = (lat, lon) => {
axios.get(
`http://api.openweathermap.org/data/2.5/weather?
lat=${lat}&lon=${lon}&APPID=${apiKey}&units=metric`
)
.then((response) => {
console.log(response.data)
return response.data
});
}
Code of the function:
You have to call the fetchFiveDayWeather with then like below.
Because the then returns a promise and it's an async call.
fetchFiveDayWeather().then( response => {
console.log(response );
})
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have the following promise
let oembedData = oembed(uuid)
.then(res => res);
console.log(oembedData);
What I'm hoping to achieve is to return the res to oembedData so the value can be used.
I know I can do
let oembedData;
oembed(uuid)
.then((res) => {
oembedData = res;
console.log(oembedData);
});
but I feel this way isn't as clean as the former example.
When I use the former my console log returns Promise {$$state: {…}}
If you're free to use async/await, it looks almost like you'd want.
async function oembed(uuid) {
return new Promise(resolve => setTimeout(() => {
resolve('foo');
}, 3000));
}
async function getOembed() {
try {
const oembedData = await oembed('1234');
console.log(oembedData);
} catch(err) {
handleError(err);
}
}
getOembed();
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
function test() {
return new Promise((resolve, reject) => {
resolve('Yay');
});
}
function func() {
return test()
.then((val) => {
return val;
})
.catch((error) => {
console.log('handle reject');
});
}
console.log(func());
I am looking for a way to get the function to return the value from the function call. Right now when we return, we actually return a promise. Whats the ideal way to go about this?
You can access the value inside a .then()
function func() {
return test()
.then((val) => {
return val;
})
.catch((error) => {
console.log('handle reject');
});
}
func().then(val){
console.log(val);
}
since func() is an asynchronous call, your console.log() won't wait for it to finish , you would need to print it in a callback or in a then()