Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
How to get response from service function (axios) with one line code in React?

I'm using react.js and react-native. I use react-redux to connect my services with components. Sometimes I call same function many times with different params. I want to write this function one time and return value every I call it. All I want is like that;

let value = this.GetSomething(params);  // this is what I want.

And here is my function to call my service function;

GetSomething(params){
  this.props.GetValueFromApi(params,
        ({ VALUE }) => {
            const status = new Promise(
                (resolve, reject) => {
                    try {
                        resolve('Operation start')
                    }
                    catch {
                        reject('Operation failed')
                    }
                });
            status.then(() => {
                console.log("VALUE", VALUE);
            })
        },
        () => {
            alert('error');
        }
    ) 
}

Here is my service function in another File.js and I'm calling it with redux connect.

export const GetValueFromApi =(PARAMS,SuccessOperation,FailedOperation) =>{
return() =>{
    const body = { params: PARAMS };
    axios.post('/api/GetValueFromApi',body)
    .then(async response => {
        SuccessOperation({VALUE: await response.data});
    })
    .catch((error)=>{
        FailedOperation(error)
    })
}

}

All I want to get response value like this let value = this.GetSomething(params); after created function GetSomething() one time top of the project.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda