Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda