Created
July 7, 2019 10:55
-
-
Save nuts-n-bits/4b879484daff4d6520148fa006cf7571 to your computer and use it in GitHub Desktop.
Random useless historic dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const 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; | |
})(); | |
Go["tester"]= function(reso, reje, psth) { | |
if(! top["i"]) | |
top["i"] = 0; | |
let comm_no = ++top["i"]; | |
console.log("starting communication #" + comm_no); | |
setTimeout(function(){ | |
console.log("heared back from communication #" + comm_no); | |
reso(); | |
}, 2000) | |
}; | |
export default Go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment