0

Am developing backend of an app using nodejs+express and using RSVP for promises.

Problem : whenever there is an error or exception in one of the functions that is unexpected the execution is stuck. Is there any way i can find which one of the functions have the error.

The code looks like this :

    function checkifvalid_call() {
        return new RSVP.Promise(function(fulfil, reject) {
            common.checkifvalid(test, function(isValid) {
                if (isValid === false) {
                    logger.fatal("Not valid test....sending invalid result")
                    res.send({
                        "Error": "Invalid test"
                    });
                }
            });
        });
    }


    function secondtest_call() {
        return new RSVP.Promise(function(fulfil, reject) {
            common.secondtest(test, function(isValid) {
                if (isValid === false) {
                    logger.fatal("Not valid second test....sending invalid result")
                    res.send({
                        "Error": "Invalid second test"
                    });
                }
            });
        });
    }

    RSVP.allSettled([checkifvalid_call(), secondtest_call()]).then(function(ret_array) {
        console.log("Calls finished");
        logger.debug("sending result..............")
        res.send("success);
    }).catch(function(reason) {
        logger.debug("Exception encountered : " + reason);
    }).finally(function() {
        logger.debug("All done finally")
    });

    RSVP.on('error', function(reason) {
        console.assert(false, reason);
    });

Thank you.

Steve
  • 19
  • 1
  • 1
  • 6
  • Uh, it doesn't seem to matter what happens in there or whether there's an unexpected exception - your promises are *always* stuck? You never resolve nor reject them! – Bergi Jul 03 '15 at 10:09
  • If those `common` methods are asynchronous, why don't they return promises? – Bergi Jul 03 '15 at 10:10
  • Thanks Bergi... I was missing the reject/fulfill in one of the methods which caused the web service call to never return. But i still have the doubt that if a particular function is taking too long to return maybe because of a long running database query then how do i come to know which function of all the ones passed to RSVP is causing the delay. Whats the most elegant method to check this? – Steve Jul 03 '15 at 11:31
  • You can give each of them a timeout and report when it's not met – Bergi Jul 03 '15 at 11:46

1 Answers1

0

Try using try{}catch, and use RSVP's error handler on each of the promises instead of the entire thing. The current set of handlers only checks if 'allSettled' has an error afaik.

This blog post explains promises very well:

How To Use Promises

Ahmad
  • 12,336
  • 6
  • 48
  • 88