Skip to content

Instantly share code, notes, and snippets.

@getify
Last active January 4, 2016 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/8572687 to your computer and use it in GitHub Desktop.
Save getify/8572687 to your computer and use it in GitHub Desktop.
// the old way :/
$.ajax({
url: "http://codepen.io/chriscoyier/pen/b2ad8b9a336845dd7b11d4bed34f4256.html",
complete: function(resp) {
// This HTML needs to be there first
$("body").append(resp.responseText);
// Because this script is going to do something with it.
$.getScript("http://codepen.io/chriscoyier/pen/cee78be969f36ddb01de3d74fd46b451.js");
// But that feels like it could be faster because why not do both requests right away but execute in order (or whatever).
}
});
// better, and more promise like... :)
// but, still "serial"... :(
$.ajax({
url: "http://codepen.io/chriscoyier/pen/b2ad8b9a336845dd7b11d4bed34f4256.html"
})
.done(function(resp){
// This HTML needs to be there first
$("body").append(resp.responseText);
// Because this script is going to do something with it.
$.getScript("http://codepen.io/chriscoyier/pen/cee78be969f36ddb01de3d74fd46b451.js");
});
// IF "cee78be969f36ddb01de3d74fd46b451.js" had a function
// in it to call when you wanted it to apply, like `myScriptBegin()`,
// instead of automatically causing side effects on the page
// when it finishes loading, then you can do them in parallel.
// here's how you do THAT with asynquence:
ASQ()
// in asynquence, a gate is two or more things happening in
// parallel, and we have to wait for both to finish (in either
// order) before we move on
.gate(
// one thing in parallel
function(done){
$.ajax({
url: "http://codepen.io/chriscoyier/pen/b2ad8b9a336845dd7b11d4bed34f4256.html"
})
.then(done,done.fail);
},
function(done){
$.getScript("http://codepen.io/chriscoyier/pen/cee78be969f36ddb01de3d74fd46b451.js")
.then(done,done.fail);
}
)
// this step will wait for the previous gate to finish before it goes on
.val(function(resp){
// This HTML needs to be there first
$("body").append(resp.responseText);
// Because this script is going to do something with it.
myScriptBegin(); // this is the function in your script that activates the page
})
.or(function(err){
console.log("Oops, something broke: " + err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment