Last active
January 4, 2016 05:08
-
-
Save getify/8572687 to your computer and use it in GitHub Desktop.
answering from chris coyier's http://codepen.io/chriscoyier/pen/dKnpa?editors=001
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
// 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). | |
} | |
}); |
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
// 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"); | |
}); |
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
// 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