2
this.custFunc = Bluebird.method(function (id)) {

    var interval = setInterval(function () {
                calc(id)
                .then(function (flag) {
                    if (flag) {
                        execProg(id, interval);
                    }
                })
                .catch(function (err) {
                    return Bluebird.reject(err);
                });
            }, pollingInterval);
});

How do I catch the error being thrown inside the setinterval function? I am not able to figure it out?

user1692342
  • 5,007
  • 11
  • 69
  • 128

1 Answers1

0

How do I catch the error being thrown inside the setinterval function?

You cannot. It's an asynchronous callback, and promises don't catch those. You could be explicitly doing try catch (or Promise.try), but that's not the right solution. See also here.

I am not able to figure it out

setInterval doesn't work well together with promises, which represent a single result. Your custFunc doesn't return a promise for the end of the interval, which it is supposed to be.

You can make everything much simpler by refactoring this to a recursive solution:

this.custFunc = function(id) {
    return Promise.delay(pollingInterval).then(function() {
        return calc(id);
    }).then(function(flag) {
        if (flag) {
            return execProg(id);
        }
    }).bind(this).then(function() {
        if (/*you want to break the interval*/)
            return result;
        else
            return this.custFunc(id);
    });
});

You might need to restructure execProg a bit to signal continue/break as a return value instead of by calling clearInterval - or alternatively just make the recursive call from within execProg.

If you depend on the steady timing of setInterval (without being influenced by the execution time of the async(!) calc and execProg functions), you might want to call Promise.delay immediately and cancel it later if no longer needed. Alternatively, implement an accurate timer.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @BenjaminGruenbaum: thanks, embarrassing mistake :-/ – Bergi Jul 15 '15 at 21:07
  • Any time, just waiting for OP to confirm this is an OK alternative to `setInterval` before voting :) – Benjamin Gruenbaum Jul 15 '15 at 21:07
  • @BenjaminGruenbaum: Why wouldn't it? OK, `execProg` will need to be restructured a bit to signal *continue* / *break* as a return value vs. calling `clearInterval`, but I don't see any other proper solution. – Bergi Jul 15 '15 at 21:09
  • Well, for one thing `setTimeout` with recursion and `setInterval` [behave differently](http://stackoverflow.com/a/729943/1348195) in how they execute. This is possible with `setInterval` but you'd have to use a promise constructor that rejects when any promise rejects and resolves when the interval is cleared (through a second interval checking that, probably). – Benjamin Gruenbaum Jul 15 '15 at 21:11
  • @BenjaminGruenbaum: To my knowledge, `setInterval` drifts as much as `setinterval`, though it doesn't depend on the timing between the calls. I'll amend my answer though. – Bergi Jul 15 '15 at 21:14
  • Your answer is fine, OP just needs to clarify :) They both "drift" but setTimeout's drifts aggregate afaik. – Benjamin Gruenbaum Jul 15 '15 at 21:15
  • @BenjaminGruenbaum: Ah, but you've raised an important point, as `calc` and `execProg` are supposedly asynchronous, the recursive `setTimeout`/`delay` drifts really hard. – Bergi Jul 15 '15 at 21:24