P
P
Pavel_Dorozhkin2020-09-02 23:12:40
React
Pavel_Dorozhkin, 2020-09-02 23:12:40

How to resolve a promise in another React component?

Hello, tell me where to dig?)) There is a function. In the function itself, if you write then, everything will resolve.

getData(startUrl).then((data) => {
    let promises = [];

    data.starships.forEach(starship => {
      promises.push(getData(starship));
    });

    return Promise.all(promises);
  });


There is a react component, I connect this function through the context, in the useEffect hook I try to get data, but I get
SyntaxError: Unexpected token < in JSON at position 0.

const { getData } = useContext(ApiContext);

useEffect(() => {
    getData().then(data => {
      console.log('data: ', data);
    }).catch(e => {
      console.log(e);
    })
  }, [getData])


ps in the component, the function is connected and exists)) Thank you in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-09-02
@hzzzzl

SyntaxError: Unexpected token < in JSON at position 0.

and what comes in response in this call, if you look in the chrome console -> network?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question