I am trying to get the promises
to resolve correctly,while debugging the worker
,the worker sends the payload using onmessage
.
I create a worker using a singleton
,the callWorker
creates a promise
which is resolved in onmessage
.
var checkIntersectionWithAllLayers = function checkIntersectionWithAllLayers(rectFeature) {
var callWorker = function callWorker(layerLabel) {
var deferred = Q.defer();
var payload = {
type: 'start',
payload: {
features: layerInfo.getAt(layerLabel).data.toGeoJSON().features,
rectFeature: rectFeature,
layerLabel: layerLabel
}
};
worker.onmessage = function getIntersectingFeatures(e) {
if (e.data.type === 'rectangle_query_worker_result') {
var result = e.data.result;
console.log("The result for layerLabel", result.layerLabel, "is ", result);
deferred.resolve(result);
}
};
worker.postMessage(payload);
return deferred.promise;
};
var promises = _(layerInfo.get())
.pluck('layerLabel')
.filter(layerData.isLayerChecked)
.map(callWorker)
.thru(Q.all)
.value();
return promises;
};
I use the promises like this:
var resolveIntersections = function resolveIntersections(intersections) {
console.log("The intersections resolved by the code",intersections);
map.fire("rectangle_query_results", {
data: intersections
});
};
var promises = checkIntersectionWithAllLayers(rectFeature);
promises
.then(resolveIntersections)
.fail(function (error) {
console.error(error, '\n', error.stack);
});
This code works,I want to get the promise working:
console.log("The result for layerLabel",result.layerLabel,"is ",result);
This code is never called:
console.log("The intersections resolved by the code",intersections);
The Chrome Devtools Promises
tab does not seem to capture the information from this promise.