注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

let Go = (function(){

    let Go = function(...fns){

        //secure this
        let instance = this;

        // constructor
        let task = new Promise(resolve => resolve());

        // a private function
        let chain = function(fn){

            let f2;

            let promise = new Promise((reso, reje) => {
                f2 = passthrough => fn(reso, reje, passthrough)
            });

            task.then(f2);
            task = promise;

        };

        // exposing method
        instance.chain = function(...fns){

            for(let i=0; i<fns.length; i++){

                if(typeof fns[i] === "function")
                    chain(fns[i]);

                else if(typeof fns[i] === "object" && fns[i] !== null && fns[i].constructor === Array)
                    instance.chain(...fns[i]);

                else
                    throw new TypeError("Cannot chain non-function and non-arrays to a Go instance.");

            }

            // allows for chaining of chaining, i.e. go.chain(f1).chain(f2)
            return instance;
        };

        // initialization chaining
        this.chain(...fns);

    };

    return Go;

})();


// tester

Go.tester(reso, reje, psth)
{
    if(! window.i)
        window.i = 0;

    let comm_no = ++i;

    console.log("starting communication #" + comm_no);

    setTimeout(function(){

        console.log("heared back from communication #" + comm_no);
        reso();

    }, 2000)
}