// bsync (-fibers)
bsync makes using fibers and futures unbelievably easy. the syntax
is dope and simple, particularly so if you're using CoffeeScript.
for the sake of convenience,
bsync also wraps a variety of functions
from caolan/async, making the syntax
more usable from CoffeeScript (those double callback invocations were
always sort of a mess in Coffee).
bsync relies upon laverdet/node-fibers
for its under-the-hood magic.
$ npm install bsync-fibers
using one of
bsync's wrappers for the functions in
collection methods (like, say,
Array.map), but with the huge bonus
that it's still asynchronous!
bsync = require 'bsync-fibers'bsyncfffiber ->trydata = 123450mapped = bsyncmap data cbnullitem * 10consolelog "Finished with bsync.map:"mappedcatch errconsoleerror "Error mapping random data to random other data:"err
any asynchronous function taking a callback with one return value
one of node's core
fs = require 'fs'require 'bsync-fibers'fffiber ->tryexists = wwwait fsexists some_fileresolveconsolelog "The file "exists ? 'exists' : 'does not exist'catch errconsoleerror "Error in fs.exists:"err
wwwait to grab image data using the
imagemagick = require 'imagemagick'require 'bsync-fibers'fffiber ->trywwwait imagemagickidentify my_image_pathresolveconsolelog "The image dimensions are x"catch errconsoleerror "Error in imagemagick module:"err
any asynchronous function taking a callback with multiple return values
wwwait can also take an array of argument names as its first parameter.
this will cause it to spit out an object with callback values mapped to
those names. coupled with CoffeeScript's destructuring assignment syntax,
this makes for some EXTREMELY readable code.
request module has a function
request() that takes a callback
with the signature
callback(err, status, body). to capture
body, we can do the following:
request = require 'request'require 'bsync-fibers'fffiber ->trywwwait 'status''body' request ''resolveconsolelog "The request returned status . Body: "catch errconsoleerror "Request spat out an error:"err
waiting on multiple fffutures at the same time
notice the difference between
wait is simply the same
wait() method as you'll find in the
request = require 'request'require 'bsync-fibers'fffiber ->tryimg1 = fffuture 'status''image_data' request ''resolveimg2 = fffuture 'status''image_data' request ''resolveimg3 = fffuture 'status''image_data' request ''resolvewait img1img2img3images =imagespush img1getimagespush img2getimagespush img3getif images0status is 200consolelog "image1.png downloaded successfully"fswriteFile './image1.png'images0image_data# etc ...catch err# ...
or for maximum compactness (same example as above):
request = require 'request'require 'bsync-fibers'fffiber ->trywait = fffuture 'status''image_data'request ".png"resolve for i in 1...3images = futureget for future in futuresfor imagei in images when imagestatus is 200consolelog "image.png downloaded successfully"fswriteFile "./image.png"imageimage_data# etc ...catch err# ...
kinda unbelievable, eh? love that coffeescript...
bryn austin bellomy < firstname.lastname@example.org >
license (wtfpl v2)
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <email@example.com>
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- You just DO WHAT THE FUCK YOU WANT TO.