JSPM

thisable

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q39952F
  • License MIT

Promises with context.

Package Exports

  • thisable

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (thisable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Thisable

This is just Thenable with the possibility of binding context to callbacks. Yes, it breaks the Promises/A+ 1.1.1 Thenable specification, although it passes all promises-aplus-tests.

Installation

npm install thisable

Usage

The only difference between Thenable and Thisable is the .bind method which sets the this variable to be used within callbacks. If .bind is not called, it will behave just like the specification.

var Thisable = require("thisable");

var promise = new Thisable();

var object = { prop: "value" };

promise.bind(object);

promise.then(function(value) {
    // this strictEqual object
});

When chaining multiple promises, the context of the top promise is passed along to all the chained ones. Even if it is defined in some of the returned promises.

var Thisable = require("thisable");

var a = new Thisable();
var b = new Thisable();
var c = new Thisable();
var d = new Thisable();

var object = {a:1,b:2,c:3};
var otherObject = {d:4,e:5,f:6};

a.bind(object);

a.then(function(aval) {
    return b.bind(otherObject).then(function(bval) {
        // this strictEqual otherObject
        return aval + bval;
    });
}).then(function(abval) {
    return c.then(function(cval) {
        return abval + cval;
    });
}).then(function(abcval) {
    return d.then(function(dval) {
        return abcval + dval;
    });
}).then(function(abcdval) {
    // abcdval equal 10
    // this strictEqual object
});

setTimeout(function() { a.fulfill(1) },100);
setTimeout(function() { b.fulfill(2) },200);
setTimeout(function() { c.fulfill(3) },300);
setTimeout(function() { d.fulfill(4) },400);

License

MIT