Not a Propaganda Machine
    Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    serial-asyncpublic

    serial-async

    Build Status NPM version

    Serialization async function(promise)

    Example

    const transaction = require(".")();
     
    const asyncStorage = {
        storage: {
            foo: "bar",
        },
        get(key) {
            return new Promise((resolve, reject) => {
                if (key === undefined) {
                    resolve(this.storage);
                } else {
                    resolve(this.storage[key]);
                }
            });
        },
        set(key, val) {
            return new Promise((resolve, reject) => {
                this.storage[key] = val;
                resolve();
            });
        },
    };
     
    function upper() {
        return asyncStorage.get("foo").then(val => {
            // val === "bar" 
            return new Promise((resolve, reject) => {
                // looooog time 
                setTimeout(() => {
                    // if that is normal call, now asyncStorage.storage.foo is `biz`, 
                    // but val still is `bar`. 
                    resolve(asyncStorage.set("foo", String(val).toUpperCase()));
                }, 100);
            });
        });
    }
    function setBiz() {
        return asyncStorage.get("foo").then(val => {
            return asyncStorage.set("foo", "biz");
        });
    }
     
    // normal 
    Promise.all([
        upper(),
        setBiz(),
    ]).then(() => {
        console.log("normal:", asyncStorage.storage); // result: { foo: "BAR" } 
    });
     
    // use transaction 
    Promise.all([
        transaction(upper),
        transaction(setBiz),
    ]).then(() => {
        console.log("transaction:", asyncStorage.storage); // result: { foo: "biz" } 
    });

    install

    npm i serial-async

    Downloadslast 7 days

    1

    version

    1.0.0

    license

    MIT

    repository

    github.com

    last publish

    collaborators

    • avatar