Skip to main content
3 of 3
added 49 characters in body

You would possibly be better off with login, share, publish and navigate as promise-returning worker functions:

var login = function() { return isLoggedIn ? null : facebookActions.login(); }
var share = function() { return scope.sharingActions.shareOnFacebook(); };
var publish = function() { return scope.model.publish() };
var navigate = function() { return app.router.navigateToStories() };

Now you can sequence these workers with .then() as follows (or similar) :

var rootPromise = $.Deferred().resolve().promise();

if (noViews) {
    if(hasEditPermissions || !isLoggedIn) {
        rootPromise.then(login).then(share).then(publish).then(navigate);
    }
    else {
        rootPromise.then(share).then(navigate);
    }
}
else if(hasEditPermissions) {
    rootPromise.then(share).then(navigate);
}

Notes:

  • rootPromise isn't strictly necessary but keeps rest of the code tidier, particularly login().

  • login() returns null if already logged in, allowing the calling logic to be slightly simpler.

  • The whole thing may simplify further depending in part on the relationship between hasEditPermissions and isLoggedIn (See other answers for suggestions).