GithubHelp home page GithubHelp logo

eventual's Introduction

eventual

Build Status

An abstraction for eventual values.

What is an eventual value?

An eventual value is a placeholder for values that will be recieved asyncronously at some point in the future. Eventual values are typically used in place of callbacks for asyncronous operations. They allow you to return a deferred value syncronously that will be resolved asyncronously. Using deferred values allows you describe asyncronous actions in a syncronous fashion.

You may have seen eventual value abstractions before: jQuery.deferred and Promise are both abstractions layers for eventual values.

By allowing you to return a future value immediately, eventuals give you a handy way to avoid nested callback hell. They also make it easy to describe asyncronous control flows that are difficult to handle with callbacks ("do this when x and y are ready, then wait for any of a, b and c, and do that").

Install

npm install eventual

Show me how...

Let's create an eventual version of Node's readFile.

// Require our library files...
var eventuals = require('eventual'),
    defer = eventuals.defer,
    deliver = eventuals.deliver,
    when = eventuals.when;

var fs = require('fs');

// Create our readFile function.
function readFile(file, encoding) {
  // Create a deferred value.
  var deferredFileContents = defer();
  
  fs.readFile(file, (encoding || 'utf8'), function (err, contents) {
    // When file is read, deliver the contents. If there is an
    // error, deliver that instead.
    deliver(deferredFileContents, (err || contents));
  }
  return deferredFileContents;
}

var deferredFoo = readFile('foo.txt');

when(deferredFoo, myCallback, myErrback);

var deferredBar = readFile('bar.txt');

var deferredBoth = group(deferredFoo, deferredBar);
when(deferredBoth, myOtherCallback, myOtherErrback);

eventual's People

Contributors

gordonbrander avatar gozala avatar

Watchers

 avatar  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.