-1

调用此函数时,我希望该函数在返回对象之前等待回调完成r

    function getWmsLayers(oneServerState){
            reader = new GeoExt.data.WMSCapabilitiesReader();;
            var serverLayers = new Array();
            var store = new GeoExt.data.WMSCapabilitiesStore({
                    url: OpenLayers.ProxyHost + oneServerState.serverUrl,
                    reader: reader
            });
            var isCallBackCompleted = false;
            store.load({
                    callback: function (r) {
                            return r;
                    }
            });
            //Do something ??
            return r;
    };


    layers = getWMSLayers(oneServerState);
4

2 回答 2

2

您不能等待异步调用完成。这本质上就是异步调用的定义!你能做的最好的就是拆分你的调用逻辑。所以代替这个:

stuff();
result = doAsyncCall(); // WILL NOT WORK!
moreStuff(result);

使用这样的回调:

stuff();
var callback = function (result) { moreStuff(result); };
doAsyncCall(callback);
于 2013-02-22T20:13:54.913 回答
1

您必须 w8 才能加载图层。像这样:

    function getWmsLayers(oneServerState, fnCallback){
            reader = new GeoExt.data.WMSCapabilitiesReader();;
            var serverLayers = new Array();
            var store = new GeoExt.data.WMSCapabilitiesStore({
                    url: OpenLayers.ProxyHost + oneServerState.serverUrl,
                    reader: reader
            });
            var isCallBackCompleted = false;
            store.load({
                    callback: function (r) {
                        fnCallback(r);
                    }
            });
    };

然后用这个调用这个函数:

    getWmsLayers(serverstate, function ( l ) {
       layers = l;
       nextFunctiontoContinueProgressing();
    });

这就是你如何建立一个回调链来完成你的请求。

于 2013-02-22T20:16:37.663 回答