0.0.5 • Published 2 years ago

thener v0.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

Thener

Thener is a library that mimics the then portion of a promise, allowing stateful capture of a value and uni-directional transfer using functions passed to then.

const thn = new Thener(1);
const thn2 = thn.then(e => 2 * e);
console.log(thn2.value); // 2

Installing

npm install thener

Main Example

Import and Construct

import Thener from "thener";
const thn = new Thener(3);
console.log(thn.value) // 3

Then

const thn2 = thn.then(e => 2 * e);
console.log(thn2.value); // 6

Finally

thn2.finally(() => console.log("No Change to value"));
console.log(thn2.value); // 6

Then Again

const thn3 = thn2.then(e => 2 * e);
console.log(thn3.value); // 12

Errors

thn2.then(() => { throw "Er1" }).then(() => { throw "Er2" });
thn2.catch(e => console.log(e)); // print errors

API

Construction

Thener accepts any type and will store it internally. Thener's are immutable, any action will return a clone, or Thener with new value and carried over errors.

import Thener from "thener";
const thn = new Thener(3);
console.log(thn.value) // 3

Methods

then

then allows you to pass in a function to process the internal Thener data with. then checks if the passed in value is a function, if it is not, it clones the current then. then wraps your function in a try/catch block, if an error occurs, it is stored internally in an error array for processing using catch. If you would like to change what error value is stored, wrap your function in a try/catch block and throw the value you wish to capture. Errors on the current Thener will be added to the new Thener.

import Thener from "thener";
const thn = new Thener(3);
const thn2 = thn.then(e => 2 * e);
console.log(thn2.value); // 6

finally

finally allows you to execute a function that isn't connected to internal data.

import Thener from "thener";
const thn = new Thener(3);
thn.finally(e => 2*e);
console.log(thn.value) // 3

catch

catch allows you to process all of the stored errors by passing in a function.

thn.then(() => { throw "Er1" }).then(() => { throw "Er2" });
thn.catch(e => console.log(e)); // print errors

properties

value

A read only property returning the current internal value.

import Thener from "thener";
const thn = new Thener(3);
console.log(thn.value) // 3

Scripts

Testing

To run mocha/chai tests. npm run test

Examples

To run the main example. npm run ex

License

Thener.js is relased under the MIT license.

0.0.5

2 years ago

0.0.4

3 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago