0

I have parent function which return Promise. In parent function I'm using bluebird-retry package to execute another (nested) promise which just for development purposes currently always reject.

So I'm calling function parent which execute 4 times child. Parent function code is:

const parent = () => {
    return new Promise((resolve, reject, onCancel) => {
        setTimeout(async() => {
            console.log('CALLED PARENT');
            try {
                await retry(child, { max_tries: 4, interval: 500 })
            } catch (e) {
                reject(new Error('Rejected from parent function'));
            }
        },100);

        onCancel(() => {
            console.log('Registered cancel in parent function');
            retry.StopError(new Error('Stop retrying child'));
            reject(new Error('Cancelled parent'));
        });
    });
};

So if child() always reject it would try 4 times and after 2100ms (100ms timeout and 4 x 500ms interval) would reject error.

However after 2000ms I'm calling bluebird .cancel method on promise returned from parent function.

Inside above parent function I have onCancel which suppose to stop retrying and reject error. However this does not work as expected because it doesn't stop retrying. Output of my code is:

CALLED PARENT

CALLED CHILD

CALLED CHILD

CALLED CHILD

Registered cancel in parent function

Emit stop::all event

Caught stop::all event

CALLED CHILD

but it suppose to be without last 'CALLED CHILD'.

Community
  • 1
  • 1
BT101
  • 3,666
  • 10
  • 41
  • 90
  • 1
    Don't pass `async function`s around as callbacks, especially not to `setTimeout`! (Use `Promise.delay` instead). You're never calling `resolve` - this can't work. You should avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) altogether. – Bergi Jun 04 '19 at 20:05
  • I want to stop retrying when `.cancel` method and `onCancel` function is called. – BT101 Jun 04 '19 at 22:13

0 Answers0