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.