1

There is a problem There is a class for working with API

export default class RickAndMortyApi {
    api = axios.create({
        baseURL: 'https://rickandmortyapi.com/api',
    });
    getCharacters() {
        this.api.get('/character/?page=1')
            .then(response => {
                return response.data.results; //array with data. Everything works correctly
            })
    }
}

In this class, I am getting a response from the server. Everything works correctly. But when I want to get this data in a component, I get an undefined

Component:

  useEffect(() => {
               charactersRequested();
        const data = rickAndMortyApi.getCharacters();
        console.log(data); //undefind
        charactersLoaded(data);
          }, []);
your uncle
  • 43
  • 1
  • 1
  • 4
  • 4
    Duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Guy Incognito Jul 31 '20 at 20:03

0 Answers0