I'm racing three promises. One of them should never resolve unless a specific condition is met, but it still resolves itself.
myFunction()
is meant to wait for a blockchain transaction to get confirmed. It can have statuses: confirmed, unconfirmed and failed. My issue is that statusCheck
gets resolved and returns nothing when receiving "unconfirmed".
I wish to make this function strictly:
- resolve on "confirmed"
- reject on "failed"
- ignore "unconfirmed" and wait until
successListener
resolves
How should I approach this?
public async myFunction() {
const successListener = rxjs.firstValueFrom(transactionConfirmationObservable)
.then(async result => {
return result;
// Listens for transaction status change to "confirmed"
});
const failureListener = rxjs.firstValueFrom(transactionFailureObservable)
.then(async result => {
throw new Error(result);
// Listens for transaction status change to "failed"
});
const statusCheck = axios(currentTransacionStatusRequest)
.then(async result => {
if (result == "confirmed") return result;
if (result == "failed") throw new Error(result);
// Checks status in case it became confirmed/failed before the listeners opened.
//
// Expected beviour: if result is "unconfirmed" - do nothing.
// Actual behaviour: resolves even with result "unconfirmed".
});
return Promise.race([successListener, failureListener, statusCheck]);