Microsoft Bing Maps API和加载模块问题 [英] Microsoft bing maps api and loading modules issue

查看:67
本文介绍了Microsoft Bing Maps API和加载模块问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建商店定位器,并通过require加载自定义模块.自定义模块取决于Directions&来自Microsoft的搜索模块.我讨厌回调地狱,并希望在所有内容加载完毕后预加载模块,并在自定义模块上返回一个Promise和操作.

I'm building a store locator and loading a custom module via require. The custom module is dependent on Directions & Search module from microsoft. I hate the callback hell and want to pre load the modules return a promise and action on the custom module once everything is loaded.

使用bluebird来实现Promise规范,我尝试了几种方法Promise.methodPromise.promisifynew Promise(function(resolve, reject){Microsoft.Maps.loadModule({callback:resolve})}),但似乎其中的任何一种都无法正常工作.

Using bluebird for Promise spec and I've tried several approaches Promise.method, Promise.promisify, new Promise(function(resolve, reject){Microsoft.Maps.loadModule({callback:resolve})}) I can't seem to get any of them working.

我的最新实现:

function loadSearch() {
            var resolver = Promise.defer();
            Microsoft.Maps.loadModule('Microsoft.Maps.Search', {
                callback: resolver.resolve
            });
            return resolver.promise;
        } /* end loadSearch */ 

        function loadDirections() {
            var resolver = Promise.defer();
            Microsoft.Maps.loadModule('Microsoft.Maps.Directions', {
                callback: resolver.resolve
            });
            return resolver.promise;
        }

Promise.all([loadSearch(), loadDirections()], function() {
        //do something
    });

得出Uncaught TypeError: Cannot read property '_tryFollow' of undefined bluebird.js 谁能指出最新代码中明显的错误或以承诺方式加载模块的伪代码示例.

results in Uncaught TypeError: Cannot read property '_tryFollow' of undefined bluebird.js Can anyone point out what an obvious error in the latest code or a psuedo code example of loading modules in a promise fashion.

推荐答案

两件事,首先,Bluebird中的Promise.all不接受这样的第二个参数,它返回一个promise,请尝试:

Two things, first of all, Promise.all in Bluebird does not accept a second argument like that, it returns a promise, try :

Promise.all([loadSearch(), loadDirections()]).then(function(results) {
    //do something
});

更好

Promise.all([loadSearch(), loadDirections()]).spread(function(search,dirs) {
    //do something
});

第二,defer的方法未绑定到defer实例(JS具有动态this),请改为使用promise构造函数:

Second of all, the methods of defer are not bound to the defer instance (JS has dynamic this), use the promise constructor instead:

function loadDirections() {
    return new Promise(function(resolve){
        Microsoft.Maps.loadModule('Microsoft.Maps.Directions', {
             callback: resolve // since this is not a method this is not bound
        });
    });
}

以此类推.通常,与Bluebird中的deferred接口相比,最好使用promise构造函数.

And so on. generally, it is better to prefer the promise constructor over the deferred interface in Bluebird.

总计:

function loadSearch() {
    return new Promise(function(resolve){
        Microsoft.Maps.loadModule('Microsoft.Maps.Search', {
            callback: resolve
        });
    });
} 
function loadDirections() {
    return new Promise(function(resolve){
        Microsoft.Maps.loadModule('Microsoft.Maps.Directions', {
            callback: resolve
        });
    });
}
Promise.all([loadDirections(),loadSearch()]).spread(function(dir,search){
     //both done, results here
});

对于它的价值-我刚刚提出了这个问题,将来的Bluebird版本中会给出更好的错误消息.

For what it's worth - I've just raised the issue and better error messages will be given in future versions of Bluebird.

这篇关于Microsoft Bing Maps API和加载模块问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆