NESTING MADNESS

$(".button").click(function{

    $.get( "example.php", function(data) {
        $("div .content").html(data);
        $(".button").click(function{
            $.get( "example.php", function(data) {
                $("div .content .newcontent").html(data);
            });
        });
    });
});

DEBUGGING MADNESS

var ajax2 = function(data) {
    $("div .content .newcontent").html(data);
}

var button2Click = function(data) {
  $.get( "example.php", ajax2);
};

var ajax1 = function(data){
    $("div .content").html(data);
    $(".button").click(button2Click);
};

var button1Click = function(){
    $.get("example.php", ajax1)
};

$(".button").click(button1Click);

PROMISES THE WRONG WAY

$(".button").click(function{

    $.get( "example.php" ).done(function(data) {
        $("div .content").html(data);
        $(".button").click(function{
            $.get( "example.php").done(function(data) {
                $("div .content .newcontent").html(data);
            });
        });
    });
});

A PROMISE

FUNCTIONAL MADNESS

new Promise(function(resolve, reject) {

    if (true) {
        resolve()
    } else {
        reject()
    }

}

FUNCTIONAL MADNESS

new Promise(function(resolve, reject) {

    if (true) {
        resolve("It was TRUE!");
    } else {
        reject("But... HOW!?");
    }

}

FUNCTIONAL MADNESS

new Promise(function(resolve, reject) {
    $.get("example.php",
        function (data) { // Success
            resolve(data);
        },
        function (error){ // Error
            reject(error);
        }
    );
}

FUNCTIONAL MADNESS

var ajaxPromise = new Promise(
    function(resolve, reject) {
    $.get("example.php",
        function (data) { // Success
            resolve(data);
        },
        function (error){ // Error
            reject(error);
        }
    );
}

FUNCTIONAL MADNESS

var ajaxPromise = new Promise(
    function(resolve, reject) {
    $.get("example.php",
        function (data) { // Success
            resolve(data);
        },
        function (error){ // Error
            reject(error);
        }
    );
}

FUNCTIONAL MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise.then(function(message){
    console.log(message);
});

CHAIN MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise
.then(function(message){
    console.log(message);
})
.then(function(message){
    $("div.content").html(message);
})

CHAIN MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise
.then(function(message){
    console.log(message);
})
.then(function(message){
    $("div.content").html(message);
})
.then(function(message){
    alert(message);
})

CHAIN MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise
.then(function(message){ // resolved
    console.log(message);
}, function(errorMessage){ // rejected
    alert(errorMessage);
});

CHAIN MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise
.then(function(message){ // resolved
    console.log(message);
})
.catch(function(errorMessage){ // rejected
    alert(errorMessage);
});

CHAIN MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise
.then(function(message){
    return JSON.parse(message);
})
.then(function(message){
    for (m in message) {
        // ...
    }
})

CHAIN MADNESS

var ajaxPromise = new Promise(/*...*/);

ajaxPromise
.then(JSON.parse)
.then(function(message){
    for (m in message) {
        // ...
    }
})

FUNCTIONAL MADNESS

When.js

jQuery deferred

A+

resolve(

    'the end'

);

resolve(

    'the end'

);

JS Promises

By Brian Graham

JS Promises

  • 2,040