JSFiddle - React, Tailwind, and code Playground

by Eric Hynds

JavaScript

function getData(){
   return $.get('/echo/html/');
}

function sweetAnimation(){
    var dfd = new $.Deferred;

    setTimeout(function(){
        dfd.resolve( "dsa" );
    }, 1000);
    
    return dfd.promise();
}

function getSomethingSync(){
    return "foo";
}

$.when( getData(), sweetAnimation() )
    .then(function( ajaxResult ){
        console.log('so awes', arguments);
    })
    .fail(function(){
        console.log( 'one or more failed', arguments );
    });
    
    // deferred looks for a promise method, which both deferreds and ajax methods return, to determine whether or not they're observable
    
    /*
so that you can do stuff like: $.when( 1 ).then( function( value ) { value===1 } )

1 doesn't have a promise method, is not observable so when creates a deferred resolved with 1 and returns the corresponding promise

$.when( maybeAsync() ) - the idea is that some user code may return a promise OR an already existing value
    */
    
/*
    if whatever you pass to $.when should be async, it must return a promise so it can be observable.
*/