Skip to content

Instantly share code, notes, and snippets.

@laser
laser / _README.md
Last active December 31, 2015 11:59
Modifying sync (and consumer) to handle concurrent stuff

Some notes about the gist:

  • Calling "sync" creates an array to keep track of return values from any asynchronous operation.

  • The "resume" method is called in your generator code and its return value is passed as the callback to your asynchronous operation. Calling "resume" causes a counter to increment, letting your "sync" method know how many asynchronous operations are in flight.

  • The return value of "resume" is a function that causes the result of the asynchronous operation to be placed into the data structure in #1. Immediately after doing this assignment, we check to see if there are any outstanding asynchronous operations. If not, we resume the generator with either an (ordered) array containing the results of our asynchronous operations or (in the event of only one operation having been fired off)

The important thing to note is that both asynchronous operations have been fired off before yielding. Each callback (passed to these asynchronous calls) is the return value from the call to "resume" -

@laser
laser / scheduler_vanilla.js
Last active December 30, 2015 12:29
scheduler
app.get('/home', prioritize(prioritizer, responder));
var queues = [];
function flush() {
if (!flush.polling) {
flush.polling = setInterval(function() {
var x;
for (var i = 0; i < queues.length; i++) {
for (var j = 0; j < queues[i].length; j++) {
@laser
laser / async.js
Last active December 30, 2015 10:29
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);
@laser
laser / J.js
Last active December 29, 2015 11:29
J.js
sync(function* (resume) {
var oneAndTwo = yield [_get("test1.txt", resume()), _get("test2.txt", resume())]
var three = yield _get("test3.txt", resume())
log(oneAndTwo[0] + " - " + oneAndTwo[1] + " " + three)
});
@laser
laser / I.js
Created November 26, 2013 19:21
I.js
function sync(gen) {
var iterable, resume;
resume = function(err, retVal) {
if (err) iterable.raise(err); // raise!
iterable.next(retVal);
};
iterable = gen(resume);
iterable.next();
@laser
laser / H.js
Created November 26, 2013 19:20
H.js
try {
firstAsync(function(err, a) {
if (err) { console.log(err); throw err; }
secondAsync(function(err, b) {
if (err) { console.log(err); throw err; }
thirdAsync(function(err, c) {
if (err) { console.log(err); throw err; }
callback(a, b, c);
});
});
@laser
laser / G.js
Last active December 29, 2015 11:29
G.js
// **************
// framework code
function sync(gen) {
var iterable, resume;
resume = function(err, retVal) {
if (err) iterable.raise(err);
iterable.next(retVal); // resume!
};
@laser
laser / F.js
Last active December 29, 2015 11:29
F.js
function* powGenerator() {
var result = Math.pow(----10----, ----2----);
return result;
}
@laser
laser / E.js
Last active December 29, 2015 11:29
E.js
function* powGenerator() {
var result = Math.pow(----10----, yield "b");
return result;
}
@laser
laser / D.js
Created November 26, 2013 19:17
ES6 Generators
function* powGenerator() {
var result = Math.pow(yield "a", yield "b");
return result;
}
var g = powGenerator();
console.log(g.next().value); // "a", from the first yield
console.log(g.next(10).value); // "b", from the second
console.log(g.next(2).value); // 100, the result