function getRandomNumberAsync (max) {
    var d = $.Deferred();
    var runFor = 1000 + Math.random() * 1000;
    setTimeout(function() {
        var result = Math.random() * max;
        d.resolve(result);
    }, runFor);
    return d.promise();
}

getRandomNumberAsync(10).then(function(number) {
    console.log(number); // zwraca liczbę z zakresu od 0 do 10
});


function getRandomNumberAsync (max) {
    return new Promise(function (resolve, reject) {
        var runFor = 1000 + Math.random() * 1000;
        setTimeout(function() {
            var result = Math.random() * max;
            resolve(result);
        }, runFor);
    });
}

getRandomNumberAsync(10).then(function(number) {
    console.log(number); // zwraca liczbę z zakresu od 0 do 10
});
