I've written the script below. It's used to communicate with a backend service. The problem arises when I call the 'heartbeat' method. The problem is probably caused by JavaScript being asynchronous.
I've used the 'done' promise, so the request should be done before I return true or false. As of now the 'heartbeat' is just undefined when evaluated.
/**
 * This module is used to communicate with the backend.
 */
var Backend = (function() {
    /**
     * Default settings for the Backend module.
     * @type {[Object]}
     */
    var settings = {
        api: 'https://www.domain.tld/api'
    };
    /**
     * This is used to create a request against the backend.
     * @param  {[String]} method   The HTTP method to be used.
     * @param  {[String]} endpoint Endpoint to target.
     * @return {[Object]}          Returns the XHR request.
     */
    var request = function(method, endpoint) {
        req = $.ajax({
            url: settings.api + endpoint,
            type: method
        });
        return req;
    };
    return {
        /**
         * Check the backend status.
         * @return {[Bool]} Returns true or false - depending on the status.
         */
        heartbeat: function() {
            var req = request('get', '/heartbeat');
            req.done(function(data) {
                if(data.status == 'alive') {
                    return true;
                } else {
                    return false;
                }
            });
        }
    }
})();
I'm doing the following to call the method:
var backend = Backend();
var heartbeat = backend.heartbeat();
heartbeat
'undefined'
What is the reason to the 'heartbeat' variable being undefined? Is it because of the asynchronous way JavaScript works, and is there maybe a way to solve this?
 
    