Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!