0

I am attempting to fetch JSON data on multiple URLs. The URL is a single fixed string with unique keys concatenated at the end. These keys are supplied as an array which I'm iterating over using map(). The resulting data is passed into Promise.all(), but undefined is returned. I can't for the life of me figure out why?

export const fetchApiEvents = async eventIds => {
    try {
        const events = await Promise.all(eventIds.map(id => {
            fetch('http://api_url/events/' + id)
                .then(res => res.json())
        }))
        return events
    } catch (e) {
        console.log('An error occured fetching the events: ', e)
    }
}

Then I'm calling this function as an effect:

    useEffect(() => {
        fetchApiEvents(eventIds)
            .then(response => setEvents(response))
    }, [eventIds])

Am I doing something really stupid?

1
  • Does your console logging reveal anything? Commented Jun 1, 2020 at 21:51

1 Answer 1

1

return is missing in line 4:

export const fetchApiEvents = async eventIds => {
    try {
        const events = await Promise.all(eventIds.map(id => {
            return fetch('http://api_url/events/' + id)
                .then(res => res.json())
        }))
        return events
    } catch (e) {
        console.log('An error occured fetching the events: ', e)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.