New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ahead

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ahead

An experiment with promises

0.0.1
latest
npm
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

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, keep) {
    keep(a * b);
});

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

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

var pow = ahead.shift(function (a, b) {
    return Math.pow(a, b);
});

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) {
    }
);

Keywords

promise

FAQs

Package last updated on 07 Apr 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts