I am looking to extract information from various sources. Once each dataset is obtained, I want to send it to a separate asynchronous processor such as a worker thread. After all processes are complete, I aim to combine the data and deliver it.
Here is an example:
function multiplyAndSum(a1, a2){
ret = 0;
for(var i = 0, l = a1.length; i < l; i++){
ret += a1[i]*a2[i];
}
return ret;
}
function getProcessedData(){
var worker1 = new Worker('recordProcessor.js');
var worker2 = new Worker('recordprocessor.js');
var results = null;
worker1.onmessage(event=>{
results=(!results)?event.results:
multiplyAndSum(event.results, results);
});
worker2.onmessage(event=>{
results=(!results)?event.results:
multiplyAndSum(event.results, results);
});
var ds1 = fetch('./dataSet1.json')
.then(rep=>resp.json())
.then(d=>worker1.postMessage({
'set': 1,
'data': d
}));
var ds2 = fetch('./dataSet2.json')
.then(rep=>resp.json())
.then(d=>worker2.postMessage({
'set': 1,
'data': d
}));
// Once results is populated return results
}
I believe I should encapsulate all of this in another Promise, but I am unsure of the exact approach to take.