是递归的AJAX调用一个坏主意? [英] Are recursive AJAX calls a bad idea?

查看:130
本文介绍了是递归的AJAX调用一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的函数来拉模板的数组:

I have a simple function to pull in an array of templates:

function getTemplates(names, done, templates, index) {
    if (!index) index = 0;
    if (!templates) templates = {};
    if (index === names.length) return done(templates);
    $.ajax({
        url: '/templates/' + names[index] + '.min.html',
        success: function (data, status, xhr) {
            templates[names[index++]] = data;
                return getTemplates(names, done, templates, index);
        }
    });
}

这似乎合乎逻辑的我刚刚从一个转到下一个,直到他们都检索到,然后回调到调用函数。不过我很好奇,如果这样做有什么不好的副作用。我还没有看到任何到目前为止,但我不想去生产这个没有首先得到任何潜在的问题的一些见解。

It seems logical to me to just go from one to the next until they are all retrieved, then callback to the calling function. But I'm curious if doing so has any bad side effects. I haven't seen any so far, but I don't want to go to production with this without first getting some insight on any potential problems.

更新: 随着谷歌和BenjaminGruenbaum的帮助下,我已经设计了一个解决方案:

UPDATE: With the help of Google and BenjaminGruenbaum I've devised a solution:

function getTemplatesAsync(names, done) {
    var calls = [];
    var templates = {};
    names.forEach(function (name, index) {
        calls.push(
            $.ajax({
                url: '/templates/' + names[index] + '.min.html',
                success: function (data, status, xhr) {
                    templates[names[index++]] = data;
                }
            })
        );
    });
    $.when.apply($, calls).done(function () {
        // using "templates" here feels fragile for some reason.  Am I wrong?
        return done(templates);
    });
}

我用模板这里,是因为我需要能够通过名字来指每个模板,但不知何故,感觉脆弱的或不可靠的。这看起来像一个安全的事是什么?

I'm using templates here because I need to be able to refer to each template by name, but somehow it feels fragile or unreliable. Does this look like a safe thing to do?

推荐答案

您更新code是比你最初的一好多了,但它仍然有一些问题,主要的一个是混合的承诺和回调,而不是使用语言设施(返回值),而不是使用映射。

Your updated code is much better than your initial one, but it still has a few issues, the main one being mixing promises and callbacks instead of using language facilities (Return value) and not using mappings.

  • 在返回的回调参数的承诺,而不是
  • 使用 .MAP ,而不是用的forEach推。
  • 使用。然后,而不是一个成功的回调,以避免两个处理同样的事情和潜在不确定的行为(做了第一次的时候执行?是否成功:
  • returning the promise instead of a callback argument
  • using .map instead of forEach with push.
  • Using .then instead of a success callback for avoiding two handlers for the same thing and potentially unspecified behavior (does the when execute first? Does the success:?)
function getTemplatesAsync(names) {
    return $.when.apply(null,names.map(function (name, index) {
        return $.get('/templates/' + names[index] + '.min.html');
    }).then(function(results){
         // map arguments to names
         return Array.prototype.reduce.call(arguments, function(obj,cur,idx){
               obj[names[idx]] = cur;
               return obj;
         },{});
    });
}

,它可以让你做的:

getTemplatesAsync(names).then(function(templates){
     // access templates here
});

这篇关于是递归的AJAX调用一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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