How to simply express a desire to run a function when two properties are true? require([
    'jquery',
    'underscore',
    'backbone'
], function ($, _, Backbone) {
    'use strict';

    //  TODO: Would like to access through define module, but not sure how..
    var player = chrome.extension.getBackgroundPage().YouTubePlayer;
    var user = chrome.extension.getBackgroundPage().User;

    //  If the foreground is opened before the background has had a chance to load, wait for the background.
    //  This is easier than having every control on the foreground guard against the background not existing.
    if (user.get('loaded')) {
        loadForeground();
    } else {
        user.on('change:loaded', function (model, loaded) {
            if (loaded) {
                loadForeground();
            } else {
                //  Show refreshing message?
            }
        });
    }

    function loadForeground() {

        if (player.get('ready')) {
            //  Load foreground when the background indicates it has loaded.
            require(['foreground']);
        } else {

            player.once('change:ready', function () {

                //  Load foreground when the background indicates it has loaded.
                require(['foreground']);
            });
        }

    }
});


My goal is to call require(['foreground']) once the user object is loaded and the player is ready. This code works fine, but seems really unclear / overly verbose.

Am I missing an easy simplification in trying to express this need?
jQuery.Deferred, or even just promises in general, would make your code simpler. In short, a promise can be unfulfilled, fulfilled, or failed. jQuery.when lets you compose multiple promises into one promise that's fulfilled when they're all fulfilled or failed as soon as one fails. This means that you can


create Deferreds for each event you want to monitor:

var userLoaded = new $.Deferred();
var playerReady = new $.Deferred();

Resolve the promises when whatever event they represent has completed, e.g.:

userLoaded.resolve();


If there's an error, you can signal that if you want as well:

userLoaded.reject();

Compose them together:

$.when(userLoaded, playerReady).then(function() {
    require(['foreground']);
});


If you pass a second argument to then, it will be called if either of the promises are rejected, simplifying error-handling, should you wish to do that.