Skip to content

Instantly share code, notes, and snippets.

@laser
Last active December 30, 2015 10:29
Show Gist options
  • Save laser/7815832 to your computer and use it in GitHub Desktop.
Save laser/7815832 to your computer and use it in GitHub Desktop.
Parallel and then series, in JavaScript (Promise, async, generators, POJS)
// async
async.waterfall([
function(callback) {
async.parallel([
function(callback) {
_get("/token", callback);
},
function(callback) {
_post("/key", callback);
}
] callback)
},
function(data, callback) {
_post("/login", data, callback);
},
], function(auth, callback) {
window.location.href = "/dashboard?" + auth;
});
// using generators
sync(function* (resume, pResume) {
var [token, key] = yield [_get("/token", pResume()), _post("/key", pResume())]; // parallel
var auth = _post("/login", data, resume);
window.location.href = "/dashboard?" + auth;
});
// vanilla
_get("/token", function(err, token) {
login.token = token;
if (login.token && login.key) login();
});
_post("/key", function(err, key) {
login.key = key;
if (login.token && login.key) login();
});
function login() {
_post("/login", [login.token, login.key], function(err, auth) {
window.location.href = "/dashboard?" + auth;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment