Node.js Promises using when/then

Doing tasks then doing other tasks... then doing other tasks... is a core problem solved by asynchronous programming. Node.js, in combination with some additional libraries, offers a vehicle for unhooking an application workflow from sequential processes. Here's a quick look at a script demonstrating the deferring of certain tasks until others have completed. In this example, the output in the console would be:

bam
i said: bam

After one second "bam" is printed. Then another second later "i said: bam" is printed.

when = require('when');

function fakePromise(value) {
    var deferred = when.defer();
    setTimeout(function() {
        deferred.resolve(value);
    }, 1000)
    return deferred.promise;
}

when(
    fakePromise('bam')
).then(
    function yes(result) {
        console.log(result);
        return fakePromise('i said: '+result);
    }
    , function no(err) {
        console.log(err);
    }
).then(
    function yes(result) {
        console.log(result)
    }
    , function no(err) {
        console.log(err)
    }
);