Git Product home page Git Product logo

tinp's Introduction

tinp

Tinp Is Not Promises.

About

What is a tinp? A tinp is not a promise as in Promises/A+ because all the complicated programming theory hurts my brain. It could stand for Thenables Is Not Promises, but that would not be gramatically correct. So what is it? It is something that can be used for turning this code:

var someTask=new SomeTask();

someTask.on("complete", function(result) {
    // handle completion.
});

someTask.on("error", function(reason) {
    // handle error.
});

someTask.do();

Into this:

var someTask=new SomeTask();

someTask.do().then(
    function(result) {
        // handle completion.
    },

    function(reason) {
        // handle error.
    }
);

Nothing more, nothing less. And as such, I find it useful.

It can in many cases be used together with other stuff that accepts a thenable.

How to use

First, install it with:

npm install tinp

Use it like so:

var Thenable=require("tinp");

function MyOperation() {
}

MyOperation.prototype.do=function() {
    this.thenable=new Thenable();

    // Initiate the operation, whatever it may be...

    return this.thenable;
}

MyOperation.prototype.someListener=function() {
    // Some time at a later stage we want to signal completion.
    this.thenable.resolve(result);
}

Why?

So why create such a thing? First, I want to say that I do understand that I deliberatly miss the point with Promises, as explained in this article. Promises is a beautiful concept. But I find that it is beautiful, kind of in the same way as LISP is beautiful. That is to say, it is beautiful, but that beauty does not neccesarily imply usefulness. I do like the syntax with then however, and I found myself frequently use it for what is referred to in the article as callback aggregation. So I decided to create this little thing to do that and only that.

Why not rely on a full implementation, even if I just use a subset? This is because, if I used for example Q, it would look like this:

var deferred = Q.defer();
FS.readFile("foo.txt", "utf-8", function (error, text) {
    if (error) {
        deferred.reject(new Error(error));
    } else {
        deferred.resolve(text);
    }
});
return deferred.promise;

So, the thing that is a Q is not actualy a Promise, but rather it containse a promise. IMO, this is unnecesary syntax and complexity. With Tinp, it would look like:

var t = Thenable(); // or var t = new Thenable();, either of them works.
FS.readFile("foo.txt", "utf-8", function (error, text) {
    if (error) {
        t.reject(new Error(error));
    } else {
        t.resolve(text);
    }
});
return t;

What's the difference?

What is the difference between thee pseudo Promises and real Promises?

  • The function then can only be called once.
  • The then function does not return anything useful.

Reference

In short, if we have:

var Thenable = require("tinp");

var t = new Thenable();

Then:

  • t.then(function() {}, function() {}) - Register resolution handlers.
  • t.resolve(result) - Resolve the Thenable with result.
  • t.reject(reason) - Reject the Thenable because of reason.
  • Thenable.resolved(result) - Create a resolved thenable.
  • Thenable.rejected(reason) - Create a rejected thenable.
  • Thenable.all(a,b,c,...).then() - Wait for all to resolve. Reject if any rejects.
  • Thenable.race(a,b,c,...).then() - Wait for any to resolve. Reject if all rejects.

tinp's People

Contributors

limikael avatar

Watchers

James Cloos 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.