Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 vendor
Octocat-spinner-32 index.js
Octocat-spinner-32 package.json
Octocat-spinner-32 readme.markdown
readme.markdown

unmaintained and dead, use at your own risk :D

promise

promise is a libary for doing async programming and it helps you to write generic algorithm's against async code and gives you a way of doing error handling with the options to fix the problems from above

examples:

simple implementation of a promise to read a file

var fs = require("fs");
var ps = require("promise");

var readFile = function(path) {
  return ps.promise(function(cont) {
    fs.readFile(path, function (err, data) {
      if (err) {
        cont.throwError("file-read.fs", { error: err });
      } else {
        cont(data);
      }
    });
  });
};

a use of a generic algorithm like syncArray

var filePaths = ......; // some array of file paths
var files = ps.syncArray(map(filePaths, readFile));

joining together 2 promises executing in parallel

var res = asyncThing1().joinWith(asyncThing2(), function(a1, a2) {
  return a1 + a2;
});

using the context feture to decide wheter to log some data

var fetchUserProfile  = function(userId) {
  return http.get("...some url?id=" + userId)
    .maps(parseUser, function(u) { return u.profileUrl; })
    .bind(http.get)
    .interact(function(data, cont) {
      if(cont.context("debug") == true) {
        console.log("fetched user profile: " + data.name);
      }
      cont(data);
    });
 };

 ......

 var profile = fetchUserProfile(1)
  .context("debug", true);

 profile(function(userProfile) {
   ......
 });

catching errors and responding to them

var refreshProfile = function(oldProfile) {
  return http.get(oldProfile.url)
    .catching("timeout.core", function(error) {
      error.continueWith(oldProfile);
    });
};
Something went wrong with that request. Please try again.