Created
April 17, 2013 14:42
-
-
Save juandopazo/5404857 to your computer and use it in GitHub Desktop.
Using AsyncQueue and Promises to control the order in which expensive assets are loaded in a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AsyncQueue version | |
YUI().use('async-queue', function (Y) { | |
var queue = new Y.AsyncQueue( | |
function () { | |
queue.pause(); | |
// Load a widget | |
YUI().use('tabview', function (Y) { | |
var tabview = new Y.TabView(); | |
//... | |
// continue to the next step | |
queue.run(); | |
}); | |
}, | |
// ... | |
function () { | |
queue.pause(); | |
Y.Get.js('http://twitter.com/some/widget.js', function () { | |
queue.run(); | |
}); | |
} | |
); | |
queue.run(); | |
}); | |
// Promises version | |
YUI().use('promise', function (Y) { | |
var step = new Y.Promise(function (success) { | |
YUI().use('tabview', function (Y) { | |
var tabview = new Y.TabView(); | |
// ... | |
success(); | |
}); | |
}); | |
step.then(function () { | |
return new Y.Promise(function (success) { | |
YUI().use('datatable', 'datasource', function (Y) { | |
// ... | |
success(); | |
}); | |
}); | |
}).then(function () { | |
return new Y.Promise(function (success) { | |
Y.Get.js('http://twitter.com/some/widget.js', function () { | |
success(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment