简体   繁体   English

需要一些帮助制作一个使用.then的承诺函数

[英]Need some help making a promise function to use a .then

I am currently working on a "pre made" project, so I am trying not to change too much of it. 我目前正在制作一个“预制”项目,所以我不想改变太多。

I have a promise function that calls an API and get an array of objects, when creating a map, but it takes too much and it is called everytime a map needs to be created: 我有一个promise函数,它在创建地图时调用API并获取一个对象数组,但它需要太多而且每次需要创建地图时都会调用它:

self.getStoreMapById = function(mapsId) {
      var deferred = $q.defer();
      $http.get("/api/stores/"+mapsId+'/map')
      .then(function (response) {
        deferred.resolve(response);
      });
      return deferred.promise;
    }

(This is how it gets called: (这就是它的调用方式:

Stores.getStoreMapById(CONFIG.map.id).then(function(mapResp){

So, to avoid waiting a lot of time, I am calling that function once, when the app begins and save the data in 3 different global variables 所以,为了避免等待很多时间,我在应用程序开始时调用该函数一次并将数据保存在3个不同的全局变量中

if(maplevel1 == undefined ){
            Stores.getStoreMapById(24).then(function(mapResp){
                maplevel1 = mapResp.data;
            });
        } //same for maplevel2 and 3

now, what I want to do is, instead of using this Stores.getStoreMapById to call it everytime I need to create a map, I want to have a function that checks the id and assing the data to another var: 现在,我想要做的是,而不是每次我需要创建一个地图时使用这个Stores.getStoreMapById调用它,我想要一个函数来检查id并将数据分配给另一个var:

if(mapsId == "24"){
            data = maplevel1;
        }
        if(mapsId == "23"){
            data = maplevel2;
        }
        if(mapsId == "21"){
            data = maplevel3;
        }

Is there a way to write that in a promise function so I can call: 有没有办法在promise函数中编写它,所以我可以调用:

assingMap(CONFIG.map.id).then(function(mapResp){

and keep the rest of the code unchanged? 并保持其余代码不变?

Thanks! 谢谢!

Yes, you return a resolved promise containing the map: 是的,您返回包含地图的已解决承诺

function assignMap(mapsId) {
    var map = /*...find the map in your variables...*/;
    return $q.resolve(map); // With ES2015's promises, this would be
                            // `return Promise.resolve(map);`
}

That uses $q.resolve to get a pre-resolved promise. 这使用$q.resolve来获得预先解决的承诺。 (This is an alias for $q.when ; it's there to maintain naming consistency with ES2015's Promise.resolve(value) .) (这是$q.when的别名;它可以保持与ES2015的Promise.resolve(value)命名一致性。)

A key aspect of promises is that the then callback is always called asynchronously, even if the promise is already resolved, so code using this won't suddenly get different timing than it used to (other than that the delays will be shorter). promises的一个关键方面是,即使promise已经被解析,也总是异步调用then回调,因此使用它的代码不会突然得到与以前不同的时序(除了延迟会更短)。


Re the "find the map in your variables" part, while you could use the long if/else if sequence you listed, you probably instead want to use a map of maps: 重新“在你的变量中找到地图”部分,虽然你可以使用你列出的长if/else if序列,但你可能想要使用地图地图:

var maps = Object.create(null);
maps[24] = maplevel1; // You can do away with the maplevel1, maplevel2,
maps[23] = maplevel2; // and maplevel3 variables entirely if you create
maps[21] = maplevel3; // the map in the code getting these up front

Then: 然后:

var map = maps[mapsId];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM