sahat/satellizer

Incorrect promise chain resolution

ViieeS opened this issue · 1 comments

I trying to make simple chain of promises like this:

var LinkAccountWithAnalytics = function (projectId) {
        $log.debug('OAuth Google Analytics ...');

        return $auth.link('googleAnalytics', {projectId: projectId}).then(function (data) {
            $log.debug('OAuth Google Analytics done.');
            return data;
        }).catch(function (r) {
            $log.debug('OAuth Google Analytics fail.');
            $log.debug(r);
            return r;
        });
    };

LinkAccountWithAnalytics($scope.project.id).then(function (d) {
                console.log('resolved.');
                console.log(d);
            }).catch(function (e) {
                console.log('rejected');
                console.log(e);
            });

console log:

// OAuth Google Analytics ...
// OAuth Google Analytics fail.
// Error: The popup window was closed
// ... stack trace
// resolved.
// Error: The popup window was closed
// ... stack trace

As you can see all works fine except second promise. If I close a popup, rejection will work only for first promise of the chain. All good with success scenario.

Workaround, wrap with $q:

var LinkAccountWithAnalytics = return function (projectId) {
        $log.debug('OAuth Google Analytics ...');

        return $q(function (resolve, reject) {
           

            $auth.link('googleAnalytics', {projectId: projectId}).then(function (data) {
                $log.debug('OAuth Google Analytics done.');

                resolve(data);
            }).catch(function (r) {
                $log.debug('OAuth Google Analytics fail.');
                $log.debug(r);

                reject(r);
            });
        });
    };