Where are my closures?

50 views Asked by At

I have a relatively simple example for you to explain the weird issue I'm seeing:

    this.applyActions = function (name, arg1, arg2, arg3, arg4, arg5, arg6) {

        var promise = new RSVP.Promise(function (resolve, reject) {
            // resolve all the functions
            RSVP.hashSettled(this.actions[name])
                //if it fails, just resolve to an empty object
            .catch(function (reason) {
                resolve({});
            })
                //resolve the functions into their promises, as designed
            .then(function (funcs) {
                for (var key in funcs) {
                    console.log(key);
                    funcs[key] = funcs[key].value(arg1, arg2, arg3, arg4, arg5, arg6);
                }
                return funcs;
            })
                //fulfill all the promises to get the results
            .then(function (promises) {
                return RSVP.hashSettled(promises);
            }, function (reason) { console.log(reason); })
            .then(function (results) {
                resolve(results);
            });;
        }.bind(this));

        return promise;
    };

Basically, when rsvp does it's thing, it may be quite some time (300 ms or more) between defining and resolving the function. In other words, by the time we've reached the the part where the functions are resolved, function ApplyAction arguments have lost their scope and have been garbage collected.

I've been wracking my brain trying to think of a way to either prevent GC (and keep my closures) or get the arguments to the anonymous functions.

Does anyone have an idea on how to solve this?

0

There are 0 answers