How to return the value of a file outside its scope

Asked

Viewed 116 times

-4

[I hate javascript/typescript] Here’s the thing, I have a problem where I need to get the result of a file with Axios and return to a function in React. Is there any way to do that ?

inserir a descrição da imagem aqui

I want to return the value in the Resp variable.

  • Export the function and import it where you want to use it

3 answers

0

It would look something like:


const suaFuncao = async () => {
  const response = await axios.get("url_sua_api");

  const { data } = response.data

  return data;
}

0

Xios works with promisse, so you need to wait for promisse to resolve at some point.

Test this code, this way you solve within the function itself:

static async findCursoPessoa(coddisciplina: string){
    try {
        coddisciplina = coddisciplina.replace('/', '-');
        const { data } = await Axios.get(`/pessoal/pessoa/${coddisciplina}/buscarCursos`);
        return data;
    } catch (error) {
        return error; 
    }
}

0

const function= async()=> {
 const response = await axios.get("localhost");
 const {data} = response.data
 return data;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.