Hi,
I want to set an interval task in a fulfilled function of then().
But, setInterval callback is not invoked.
Please tell me how i could do that?
export default {
method1: function (params) {
return eventQuery.run(params)
.then(() =>{
return new Promise((resolve, reject) => {
setInterval(() => {
console.log('in intervalCallback');
return checkEventStat.run(params)
.then(() => {
if(checkEventStat.data[0].stat == 'success') {
resolve();
} else {
reject();
}
})
}, 10000, 'testInterval');
})
})
.then(() => {
// some code
})
.catch((err) => {
return err;
})
.finally(() => {
clearInterval('testInterval');
})
}
}
Hello @srrrrrr27
can you please try not using the new Promise and just directly call setInterval and on success check you can showAlert and clearInterval. you can change the time accordingly in the below function. Let me know if this works else will look into another way.
method1: () => {
return Api1.run().then(() => {
setInterval(() => {
console.log('** in intervalCallback');
return Api2.run()
.then(() => {
if(Api2.data) {
showAlert("hey");
clearInterval("testInterval");
} else {
clearInterval("testInterval");
}
})
}, 10, 'testInterval');
})
}
2 Likes
@ Apeksha_appsmith
Thanks for your answer.
I tried as you told.
And the callback is successfully invoked.
But, still there is a problem.
That cannot access outer variable inside callback in both of promise and old direct call.
It is a bug.
https://github.com/appsmithorg/appsmith/issues/13156
Hence, the problems in my case are:
- I can’t pass any of params when call api2.run() inside callback.
- I want to clear the interval job after 10 times of repeat. but i cannot find the way to do that. Currently, i’m doing that manually.
method2: (parmas) => {
return Api1.run(params).then(() => {
let count = 0; // can't access inside callback.
setInterval(() => {
console.log('** in intervalCallback');
return Api2.run(parmas) // can't pass params into run()
.then(() => {
if(Api2.data) {
showAlert("hey");
clearInterval("testInterval");
} else {
if(count>10){ // can't limit repeat times cause cannot access count.
clearInterval("testInterval");
}
}
})
}, 10, 'testInterval');
})
}
Yes, we will be working on this bug. sorry for the inconvenience caused by it. But I will try to bump up the priority of this.
1 Like