Javascript 没有解决或拒绝的承诺
我试图使Javascript 没有解决或拒绝的承诺,javascript,promise,q,web-worker,Javascript,Promise,Q,Web Worker,我试图使承诺正确解析,在调试工作者时,工作者使用onmessage发送有效负载 var checkIntersectionWithAllLayers = function checkIntersectionWithAllLayers(rectFeature) { var callWorker = function callWorker(layerLabel) { var deferred = Q.defer(); var p
承诺
正确解析,在调试工作者
时,工作者使用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;
};
我使用singleton
创建一个worker,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
Promissions
选项卡似乎无法捕获此Promission中的信息。构建一个。在这样做的时候,你可能会发现问题所在,如果你不知道,你会有一个很好的、清晰的例子供人们帮助你。上面有太多不相关的代码。调用callWorker
的频率有多高(它映射的数组中有多少项),您多久得到一次结果Q.all
确实希望所有承诺都能得到解决,而不仅仅是其中的一部分。@Bergi the layerInfo.get()返回一个数组,其大小无法预测,我现在正在测试它与2个元素一起工作。所有承诺都必须得到解决,所有承诺的结果都传递给另一个组件以构建UI。因此,我使用Q.All
,我希望以数组的形式获得结果,因此,它比Q.allSettled
更受欢迎。我在callWorker
中有console.count
,它执行了两次。是的,但是您得到了console.log(“layerLabel的结果”,…
东西只有一次,不是吗?@Bergi没错,我只得到数组中第二个元素的响应消息。我在每个元素中添加了一个layerLabel
字符串,使它们唯一,但我仍然只得到一条响应消息。