Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created April 17, 2013 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/5404857 to your computer and use it in GitHub Desktop.
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
// 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