Giter Club home page Giter Club logo

thenable's Introduction

Thenable

Promises/A+ logo

Embeddable Minimum Strictly-Compliant Promises/A+ 1.1.1 Thenable
Copyright (c) 2013-2014 Ralf S. Engelschall (http://engelschall.com)
Licensed under The MIT License (http://opensource.org/licenses/MIT)

Abstract

This is a strictly-compliant Promises/A+ 1.1.1 implementation in just 2KB of (compressed) code, which passes the official Promises/A+ Test-Suite and includes back-references to the relevant specification statements. It just provides a minimum Promise functionality, because it is intended to be directly embedded into ECMAScript 5 based libraries and frameworks, in order to dependency-free leverage from Promises and be able to return "thenable" Promise objects to applications. As such, it only provides the bare Promise creation and resolving functionalities and no general Promise management functionalities. For applications (and similar contexts where extra dependencies do not matter), please use a full-featured Promise library like Bluebird or any of the other Promise/A+ compliant implementations instead.

Usage

Although (thanks to UMD) also usable in a stand-alone fashion in AMD, CommonJS and Browser environments, Thenable is primarily intended for embedding into dependency-free libraries and frameworks. Hence, it intentionally is neither published to the Node NPM registry nor to the Bower registry. Instead, please download the raw thenable.min.js and include it verbatim into your library/framework. The usual approach for this is to temporarily emulate a CommonJS environment within your library/framework to get its Thenable object:

var Thenable = (function () {
    var module = { exports: {} };
    /* --- START VERBATIM EMBEDDING ---- */
/*!
**  Thenable -- Embeddable Minimum Strictly-Compliant Promises/A+ 1.1.1 Thenable
**  Copyright (c) 2013-2014 Ralf S. Engelschall <http://engelschall.com>
**  Licensed under The MIT License <http://opensource.org/licenses/MIT>
**  Source-Code distributed on <http://github.com/rse/thenable>
*/
!function[...]
    /* --- END VERBATIM EMBEDDING ---- */
    return module.exports;
})();

Then to use Thenable you can do either...

myAsyncMethod = function () {
    var promise = new Thenable();
    doAsyncOperation(function onResult (result) {
        promise.fulfill(result);
    }, function onError (error) {
        promise.reject(result);
    })
    return promise.proxy();
};

...or the EMCAScript 6 style...

myAsyncMethod = function () {
    return new Thenable(function (fulfill, reject) {
        doAsyncOperation(function onResult (result) {
            fulfill(result);
        }, function onError (error) {
            reject(result);
        })
    }).proxy();
};

Features

  • strictly-compliant to Promises/A+ 1.1.1
  • source-code includes back-references to the relevant specification statements
  • bare Promise creation and resolving functionality
  • Universal Module Definition (UMD) for AMD, CommonJS and Browser support
  • optional noConflict method for non-invasive use in Browser environments
  • Static Constructor Function support
  • additional Promise Proxy support
  • additional Constructor Executor support
  • permissive MIT license
  • just 2KB of (compressed) code

thenable's People

Contributors

rse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

thenable's Issues

Resolve with `Promise` value acts different from chrome.

Hi, @rse

Nice promise/a+ implement, cause it's short. But I got a problem here.

Here is a test suit i wrote:

function test(Promise) {
    var p1 = new Promise(function(resolve) {
        var value = new Promise(function(resolve2) {
            resolve2(2);
        });
        resolve(value);
    });
    p1.then(function(value) {
        console.log(value);
    });
}
test(window.Promise); // chrome logs the number 2
test(Thenable); // thenable logs the Promise p1

I don't understand which rules the specification. But I expect the chrome result better. Any ideas?

Why not publish to NPM?

From the readme:

Thenable is primarily intended for embedding into dependency-free libraries and frameworks. Hence, it intentionally is neither published to the Node NPM registry nor to the Bower registry.

But why? I want to use this in my own Node library to make it promise-compatible, and publish it on NPM. So the obvious thing would be to add thenable as a dependency and require it, but that's not possible because thenable is not on NPM. Why?

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.