jfactory-promise

Promise where the whole tree can be awaited, canceled and expired. Provides synchronous status, explorable chain map, shared data, debug data and trace.

Usage no npm install needed!

<script type="module">
  import jfactoryPromise from 'https://cdn.skypack.dev/jfactory-promise';
</script>

README

Promise where the whole tree can be awaited, canceled and expired.
Provides synchronous status, explorable chain map, shared data, debug data and trace.

GitHub version npm version

JFactoryPromise-standalone

Standalone: This module exports the class JFactoryPromise from jfactory into a smaller standalone package. Don't use it if you are already using the full module.

Overview

<script src="https://cdn.jsdelivr.net/npm/lodash/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jfactory-promise@1.7.7/dist/JFactoryPromise-devel.umd.js"></script>
<script>
    const { JFactoryPromise } = jFactoryModule;
    let myPromise, a, b;

    // --- Await the whole tree ---

    (async function() {
        myPromise = JFactoryPromise.resolve('ok');
        a = myPromise.then(h).then(h);
        b = myPromise.then(h).then(h);
        // will expire the chain as soon as no more promises are pending:          
        myPromise.$chainAutoComplete();
        // wait for all promises         
        await myPromise.$chain;
        console.log("done");
        // chain expired, new handlers not called (passthrough):
        myPromise.then(h);
        a.then(h);
        b.then(h).then(h);
    })();

    // --- Abort the whole tree ---

    myPromise = JFactoryPromise.resolve('hello');
    a = myPromise.then(h);
    b = myPromise.then(h).then(h);
    // abort the whole tree, handlers not called:
    myPromise.$chainAbort("canceled !");
    // chain expired, new handlers not called (passthrough):
    myPromise.then(h);
    a.then(h);
    b.then(h).then(h);

    // handler
    function h(value) {/*console.log(value);*/return value}

</script>

Install

npm add lodash
npm add jfactory-promise

The package uses lodash as a peer dependency to maximize optimizations with your project (so you need to install it manually).

See also babel-plugin-lodash to reduce the size of lodash.

Automatic import

const { JFactoryPromise } = require ("jfactory-promise");
import { JFactoryPromise } from "jfactory-promise";

The package will automatically switch between development and production based on the value of process.env.NODE_ENV. Webpack automatically configures it.

Manual import:

Force development module

const { JFactoryPromise } = require ("jfactory-promise/dist/JFactoryPromise-devel.umd.js")
import { JFactoryPromise } from "jfactory-promise/dist/JFactory-devel.umd.js";

Force production module

const { JFactoryPromise } = require ("jfactory-promise/dist/JFactoryPromise.umd.js")
import { JFactoryPromise } from "jfactory-promise/dist/JFactoryPromise.umd.js";

Documentation