'How to add a promise in the redux and execute according to Data
I have a function that i am calling on a button click
const onButtonPress = () >{
storeDataInDB()
}
const storeDataInDB= () =>{
// Call multiple functions inside here and take a 1 minute to process
// Some api requests in this function also
}
So As i am clicking on the button multiple so it executes my function parallel. So want to create a redux array to store it. So if user click on the button 100 times. So it will store 100 process in the redux and execute 1 by 1 and remove it from redux as it will be done. And if i want to remove 88 index process. So i can remove it from the redux. So i am not able to get how can i store a function in this manner. Let me know if anyone can help me in this logic.
Solution 1:[1]
const storeDataInDB = new Promise((resolve, reject)=>{
// Do Call multiple functions and Api requests
resolve(data) // return value when Promise Success
reject(err) // return error value when Promise failed
}
The above code is to create a Promise and a Promise always have two conditions
- resolve (Success)
- reject (Fail)
To get values from a Promise, use .then and .catch method like following
const onButtonPress = () =>{
storeDataInDB.then((data) =>{
// return data on Promise Fulfilled
}).catch((err) => {
// return error on Promise failed
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Ahab |
