Skip to content
briancavalier edited this page Jan 10, 2012 · 5 revisions

when() + Dojo Deferred

when.js works great with Dojo's Deferred. Not only does when() accept a Dojo Deferred, all of the extra goodies that handle joining promise arrays, like when.all(), when.map(), when.reduce(), etc do too. They'll even work with a mixed array of Dojo Deferred (or deferred.promise), when.js Promise or Deferred, any other Promises/A promise, and immediate values. This is one of the advantages of when() (read more here).

when.defer() + dojo.when()

Similarly, dojo.when() (aka Deferred.when() in Dojo 1.7) can accept a when.js Promise. When you pass a when.js Promise to dojo.when(), it will also return a when.js Promise.

One key difference between when() and dojo.when(), is that when() is guaranteed to return a Promises/A compliant promise in all cases, but dojo.when() is not. dojo.when() is only guaranteed to return a Promises/A promise when it is passed one as its input.

Dojo 1.7 Builder

Here is a Dojo 1.7 Builder profile that you can use to include when.js in a build with Dojo. Thanks to bryanforbes for contributing this profile!

// Save this as when.profile.js in the when.js dir
var profile = (function(){
	var testRE = /^when\/test\//;
	return {
		resourceTags: {
            test: function(filename, mid){
				// Tag test files as such
                return testRE.test(mid);
            },
            amd: function(filename, mid){
				// Tag the module as AMD so it doesn't get
				// wrapped by the Dojo builder
                return mid == "when/when" || mid == "when";
            },
            copyOnly: function(filename, mid){
				// Don't process package.json
                return mid == "when/package.json";
            }
		}
	};
})();