Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Forked from glennblock/fibers.js
Last active December 30, 2015 08:29
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 bjouhier/7803022 to your computer and use it in GitHub Desktop.
Save bjouhier/7803022 to your computer and use it in GitHub Desktop.
//fibers
function markComplete(postedItems, completionCallback) {
var errors = [];
var getItemToUpdate = function(item) {
var fiber = Fiber.current;
process.nextTick(function() {
client.queryEntity('tasks', 'partition1', item, function(err, task) {
fiber.run(task);
});
});
return Fiber.yield();
};
Fiber(function() {
postedItems.forEach(function(item) {
task = getItemToUpdate(item);
console.log(task);
});
completionCallback(undefined);
}).run();
}
@bjouhier
Copy link
Author

bjouhier commented Dec 5, 2013

You can pass task directly from fiber.run to Fiber.yield. So you don't need an extra variable to pass the result.

You could also improve by adding a try/catch and using fiber.throwInto to propagate it.

See https://github.com/Sage/streamlinejs/blob/master/lib/fibers/runtime.js#L114-151 for a complete example

@glennblock
Copy link

Aaah, i saw I could pass to run but didn't realize I can return the yield and it would flow through. OK on the try/catch.

Thanks!
Glenn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment