An experiment with promises
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
.gitignore
Makefile
Readme.md
component.json
index.js
package.json

Readme.md

Ahead

An experiment with promises

Some examples

@see example/example.js

var ahead = require('ahead');

Ahead provides a easy way to create promises. And lets you write asynchronous stuff that looks synchronous. Its thought as an experiment and not for real use. Error handling can get pretty confusing.

It adds two arguments to a function.

  • kept to keep the promise
  • broken to break the promise
var random = ahead(function (delay, kept, broken) {
    setTimeout(function () {
        kept(Math.random());
    }, delay);
});

var r = random(10);  // creates a promise for a random number
r.then(function (number) { console.log(number);});

Functions created with ahead can take promises as arguments the execution gets deferred until all promised arguments are resolved

var multiply = ahead(function (a, b, kept) {
    kept(a * b);
});

multiply(random(10), 3); // just looks synchronous

Ahead.shift converts a synchronous function (shifts it in time ahead).

var pow = ahead.shift(Math.pow);

Lets shift console.log in time ahead

var log = ahead.shift(console.log.bind(console));

Thrown errors will break the promise

var fail = ahead.shift(function () {
    throw Error("break everything");
});

All the above created functions can be used in a synchronous style but are executed asynchronous

log(pow(multiply(random(1000), 3), 2));

The returned value of each function created with ahead or ahead.shift is a promise for a future value

fail(random(1000), 3).then(
    function success (value) {
    },
    function fail (reason) {
    }
);