npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bsync-fibers

v0.1.2

Published

Extremely easy fibers/futures library (using node-fibers). Also wraps caolan/async functions for better fiber-ized syntax.

Downloads

5

Readme

// bsync

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.

examples

caolan/async wrappers

using one of bsync's wrappers for the functions in caolan/async, we end up with syntax that looks almost identical to the core javascript collection methods (like, say, Array.map), but with the huge bonus that it's still asynchronous!

bsync     = require 'bsync'
{fffiber} = bsync

fffiber ->
  try
    data = [12, 3, 4, 50]
    mapped = bsync.map data, (item, cb) -> cb(null, item * 10)
    console.log "Finished with bsync.map:", mapped
  catch err
    console.error "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 functions:

fs                = require 'fs'
{fffiber, wwwait} = require 'bsync'

fffiber ->
  try
    exists = wwwait (resolve) -> fs.exists some_file, resolve
    console.log "The file #{some_file}", (exists ? 'exists' : 'does not exist')
  catch err
    console.error "Error in fs.exists:", err

using wwwait to grab image data using the imagemagick module:

imagemagick       = require 'imagemagick'
{fffiber, wwwait} = require 'bsync'

fffiber ->
  try
    {width, height} = wwwait (resolve) -> imagemagick.identify my_image_path, resolve
    console.log "The image dimensions are #{width}x#{height}"
  catch err
    console.error "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.

mikeal's request module has a function request() that takes a callback with the signature callback(err, status, body). to capture status AND body, we can do the following:

request           = require 'request'
{fffiber, wwwait} = require 'bsync'

fffiber ->
  try
    {status, body} = wwwait ['status', 'body'], (resolve) -> request 'http://google.com', resolve
    console.log "The request returned status #{status}.  Body: #{body}"
  catch err
    console.error "Request spat out an error:", err

waiting on multiple fffutures at the same time

notice the difference between wwwait and wait (wait is simply the same wait() method as you'll find in the fibers/futures module).

request         = require 'request'
{wait, fffiber} = require 'bsync'

fffiber ->
  try
    img1 = fffuture ['status', 'image_data'], (resolve) -> request 'http://blah/image1.png', resolve
    img2 = fffuture ['status', 'image_data'], (resolve) -> request 'http://blah/image2.png', resolve
    img3 = fffuture ['status', 'image_data'], (resolve) -> request 'http://blah/image3.png', resolve
    wait [ img1, img2, img3 ]

    images = []
    images.push img1.get()
    images.push img2.get()
    images.push img3.get()

    if images[0].status is 200
      console.log "image1.png downloaded successfully"
      fs.writeFile './image1.png', images[0].image_data
      # etc ...
  catch err
    # ...

or for maximum compactness (same example as above):

request         = require 'request'
{wait, fffiber} = require 'bsync'

fffiber ->
  try
    wait futures = (fffuture ['status', 'image_data'], (resolve) ->
      request "http://blah/image#{i}.png", resolve) for i in [1...3]

    images = future.get() for future in futures

    for image, i in images when image.status is 200
      console.log "image#{i + 1}.png downloaded successfully"
      fs.writeFile "./image#{i + 1}.png", image.image_data
    # etc ...
  catch err
    # ...

kinda unbelievable, eh? love that coffeescript...

authors/contributors

bryn austin bellomy < [email protected] >

license (wtfpl v2)

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <[email protected]>

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

  1. You just DO WHAT THE FUCK YOU WANT TO.