我正在尝试正确解析promises
,在调试worker
时,工作人员使用onmessage
发送有效负载。
我使用singleton
创建了一名工作人员,callWorker
创建的promise
已在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;
};
我使用这样的承诺:
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);
});
这段代码有效,我希望得到承诺:
console.log("The result for layerLabel",result.layerLabel,"is ",result);
永远不会调用此代码:
console.log("The intersections resolved by the code",intersections);
Chrome Devtools Promises
标签似乎无法捕获此承诺中的信息。