Giter Site home page Giter Site logo

bar.js's Introduction

Bar.js

Take your cereal bowl of callbacks and turn them into a serial bar(.js)

About

Bar.js is a simple control flow library to straighten out your callback nested code. Its inspired by coroutines as a way to create small processes that can be consumed by other coroutines. Functions can be defined that return a coroutine, which allows them to be consumed by other coroutines.

Examples

Here's a simple example for doing a simple network request.

function GetUserNameForId(userId) {
  return bar.Serial([
    function() {
      userId = userId || "cconger";

      var githubUserUrl = "https://github.com/users" + userId;
      
      return MakeNetworkConnection(githubUserUrl);
    },
    function(res) {
      res = res();
      console.log("Name", res.name);
      return res.name;
    }
  ]);
};

This expects there to be another coroutine defined called MakeNetworkConnection which opens the network connection and gives you the JSON parsed result.

We can go ahead and build that below.

First let me show you how you wrap conventional async functions. Here we'll wrap setTimeout as an example of a simple async function.

function DelayedGreeting() {
  return bar.Serial([
    function() {
      setTimeout(Bar.callback(), 2000);
      return Bar.YIELD;
    },
    function(res) {
      res = res();

      console.log("Hello, world!");
    }
  ]);
}

Here we're passing Bar.callback() as an argument to SetTimeout. It's pretty simple. Arguments that get passed to Bar.callback will be proxied across to the results passed to the following function.

Below we'll use a node library called request to implement MakeNetworkConnection.

var request = require('request');

function MakeNetworkConnection(url) {
  return bar.Serial([
    function() {
      request(url, Bar.callback(function(error, response, body) { return body; }));
      return Bar.YIELD;
    }
  ]);
};

Here we pass an argument to Bar.callback which allows us to give a function which processes the data and returns the value. This way we can do default values or other small processing things locally to make the API of your routine clean to be consumed by others. In our case, we use it to just pass the body of the response along. However usually we would want to be able to handle errors and other response irregularlites.

Tests

Run tests:

npm install jasmine-node
jasmine-node spec/

Future

TODO:

  • Add .map
  • Add .forever and .BREAK for main looping functions
  • Add CoffeeScript Wrapper
  • Write better docs
  • Write more tests

bar.js's People

Contributors

cconger avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.