Sequence.js

Examples

Passing values as arguments

(new Sequence())

.step(["hello", "world"])   // ["hello", "world"]
.step((args) => { console.log(args) })

.step(["hello world"])      // ["hello world"]
.step((args) => { console.log(args) })

.step("hello world")        // will be converted to: ["hello world"]
.step((args) => { console.log(args) })

.run();

Passing arguments from functions list

(new Sequence())

.step(() => {
    return [
        () => { return 1; },
        (cb) => { cb(2); },
        (resolve, reject) => { resolve(3); },
        4
    ];
})
.step((args) => { console.log(args) })

.run();

Using sequence as step

let sqnc1 = new Sequence();
sqnc1.step(() => { console.log("sqnc1 step1") });
sqnc1.step(() => { console.log("sqnc1 step2") });

let sqnc2 = new Sequence();
sqnc2.step(() => { console.log("sqnc2 step1") });
sqnc2.step(() => { console.log("sqnc2 step2") });

(new Sequence())
.step(sqnc1)
.step(sqnc2)
.run([]).then(() => { console.log("done") });

Using Promise as step

(new Sequence())
.step(() => {
    return new Promise((resolve, reject) => {
        resolve("hello world");
    });
})
.step(([str]) => {
    console.log(str);
})
.run();

Initial and final arguments

let subsqnc = (new Sequence()).step(([counter]) => {
    return counter + 1;
});

(new Sequence())
.step(([counter]) => { return counter + 1 }) // 1
.step(([counter]) => { return counter + 1 }) // 2
.step(subsqnc) // 3
.step(() => { return subsqnc }) // 4
.step(() => {
    // this will return [5,5,5] because each will get 4 as arg
    // and each will increase it by +1 = 5
    return [subsqnc, subsqnc, subsqnc];
})
.run([0]).then(([counter]) => {
    // 0 = initial argument
    // counter = final argument
    console.log(counter); // 5
});

Difference array / non array

(new Sequence())

// via values
.step(["i am argument #0"])
.step((arg) => { console.log(arg) })

// via values - will be converted from string to array
.step("i am argument #0")
.step((arg) => { console.log(arg) })

// via wrapper
.step(() => { return [ () => { return "i am also argument #0" } ] })
.step((arg) => { console.log(arg) })

// via wrapper - returned func will be converted to array
.step(() => { return () => { return "i am argument #0" } })
.step((arg) => { console.log(arg) })

// via wrapper - returned func (with cb) will be converted to array
.step(() => { return (cb) => { cb("and me too") } })
.step((arg) => { console.log(arg) })

.run();

Passing values to next step

(new Sequence())

.step((str) => {
    // this would just print []
    //console.log(str);
})
.step(([func]) => { /* func("this wont work") */ })

.step(() => {
    // this would stop the sequence
    // because its been treated like a function with callback
    /*
    return [
        (str) => {
            console.log(str);
        }
    ];
    */
})
.step(([func]) => { /* func("this wont work either") */ })

.step(() => {
    // this is the same as above
    // the single function will be converted to array
    /*
    return (str) => {
        console.log(str);
    };
    */
})
.step(([func]) => { /* func("this wont work") */ })

// this is the correct way:
.step(() => {
    return [
        () => {
            // here we can return the func to next step
            return (str) => {
                console.log(str);
            };
        },
        () => {
            // or an array
            return [];
        },
        () => {
            // or something else like Promise or sub sequence
            return;
        }
    ];
})
.step(([func, arr, undef]) => { func("this is ok") })

.run();

Step-by-step vs multi-step

// 3 stepFuncs: [
//  0: 1 second delay
//  1: 2 seconds delay
//  2: 3 seconds delay
// ]
let stepFuncs = [1,2,3].map((i) => {
    return (cb) => {
        setTimeout(() => {
            console.log("stepFunc " + i);
            cb();
        }, i * 1000);
    };
});

// step by step call
// output:
// (1s delay)
// stepFunc 1
// (2s delay)
// stepFunc 2
// (3s delay)
// stepFunc 3
// done
(new Sequence())
.step(() => { return stepFuncs[0]; })
.step(() => { return stepFuncs[1]; })
.step(() => { return stepFuncs[2]; })
.run([]).then(() => {console.log("done")});

// simultane call
// output:
// (1s delay)
// stepFunc 1
// (1s delay)
// stepFunc 2
// (1s delay)
// stepFunc 3
// done
(new Sequence())
.step(() => { return stepFuncs; })
.run([]).then(() => {console.log("done")});