承诺不解决或拒绝

时间:2015-06-27 05:49:40

标签: javascript promise q web-worker

我正在尝试正确解析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标签似乎无法捕获此承诺中的信息。

0 个答案:

没有答案