I want a program to run a chain of actions after certain user action is done. However, part of the chain will need to wait for the resolution of previous Promise OR the fact that user has done some action. Is it possible to make Promise work this way?
I am imagining the ideal program script is like:
var coreTrigger = Promise.any([normalAsyncRequest, userAction]);
coreTrigger.then(res=>{
// the followup action
});
...
// somewhere far away, or in developer console
userAction.done(); // I want this can be one possible path to trigger the followup action
Yes!
function createUserAction() {
let resolve = undefined;
const promise = new Promise(r => { resolve = r });
function done() {
resolve();
}
function wait() {
return promise;
}
return { done, wait }
}
And use it as you've described in your question.
const userAction = createUserAction();
var coreTrigger = Promise.any([normalAsyncRequest, userAction.wait()]);
coreTrigger.then(res=>{
// the followup action
});
// Somewhere else
userAction.done();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With