Skip to content

Instantly share code, notes, and snippets.

@Deepak13245
Created June 2, 2020 20:38
Show Gist options
  • Save Deepak13245/48e9d4cfd3af96d9db11921822ae7a16 to your computer and use it in GitHub Desktop.
Save Deepak13245/48e9d4cfd3af96d9db11921822ae7a16 to your computer and use it in GitHub Desktop.
Woker channel for parallel processing of async function
class Channel {
_results = [];
_index = 0;
constructor(list = []) {
this._list = list;
}
push(...items) {
this._list.push(...items);
}
clear() {
this._results = [];
this._index = 0;
this._list = [];
}
get results() {
return this._results;
}
next() {
if(this._list.length === 0 ) {
return [false];
}
const item = this._list.shift();
const cb = this._callback(this._index)
return [true, [ item, this._index++ ], cb]
}
_callback(index) {
return result => {
this._results[index] = result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment