Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
同步容器中异步Javascript调用的最佳实践?_Javascript_Jquery_Http_Asynchronous - Fatal编程技术网

同步容器中异步Javascript调用的最佳实践?

同步容器中异步Javascript调用的最佳实践?,javascript,jquery,http,asynchronous,Javascript,Jquery,Http,Asynchronous,假设我有一组10个对象,其中一个对象需要从一个通过Javascript调用的API中提取额外的信息。在我的例子中,这个对象是一个Soundcloud嵌入,我需要从他们的API中获得更多信息 使用jQuery,我可以使用$.getJSON并将解析的值推送到一个全局数组中,但这似乎不能保证它在没有大量拼接代码的情况下以正确的顺序放置 保持列表有序的最佳做法是什么 例如: var embeds = [{type: "mp3", url: "http://example.com/song.mp3", a

假设我有一组10个对象,其中一个对象需要从一个通过Javascript调用的API中提取额外的信息。在我的例子中,这个对象是一个Soundcloud嵌入,我需要从他们的API中获得更多信息

使用jQuery,我可以使用
$.getJSON
并将解析的值推送到一个全局数组中,但这似乎不能保证它在没有大量拼接代码的情况下以正确的顺序放置

保持列表有序的最佳做法是什么

例如:

var embeds = [{type: "mp3", url: "http://example.com/song.mp3", artist: "Artist"},
              {type: "mp3", url: "http://anotherexample.com/song.mp3", artist: "Artist 2"},
              {type: "soundcloud", url: "http://soundcloud.com/summergirlfriends/shockwaves"}];
for(e in embeds) {
    if(embeds[e].type == "soundcloud") {
        // here is where i need to call the SC API and do something with the results
    }
}

如果我理解正确,我认为您需要的是一个闭包,以确保您正在修改正确的嵌入:

var embeds = [{type: "mp3", url: "http://example.com/song.mp3", artist: "Artist"},
              {type: "mp3", url: "http://anotherexample.com/song.mp3", artist: "Artist 2"},
              {type: "soundcloud", url: "http://soundcloud.com/summergirlfriends/shockwaves"}];
for(var i=0;i<embeds.length;i++) {
    if(embeds[i].type == "soundcloud") {
        (function(embed){
            $.ajax({
               //...
               success:function(newData){
                   embed['additionalData']=newData;
                   console.log(embeds);
               }
            }); 
        })(embeds[i])
    }
}
var嵌入=[{type:“mp3”,url:http://example.com/song.mp3,艺术家:“艺术家”},
{键入:“mp3”,url::http://anotherexample.com/song.mp3,艺术家:“艺术家2”},
{类型:“soundcloud”,url::http://soundcloud.com/summergirlfriends/shockwaves"}];

对于(var i=0;i如果我正确理解了您,我认为您需要的是一个闭包,以确保您修改了正确的嵌入:

var embeds = [{type: "mp3", url: "http://example.com/song.mp3", artist: "Artist"},
              {type: "mp3", url: "http://anotherexample.com/song.mp3", artist: "Artist 2"},
              {type: "soundcloud", url: "http://soundcloud.com/summergirlfriends/shockwaves"}];
for(var i=0;i<embeds.length;i++) {
    if(embeds[i].type == "soundcloud") {
        (function(embed){
            $.ajax({
               //...
               success:function(newData){
                   embed['additionalData']=newData;
                   console.log(embeds);
               }
            }); 
        })(embeds[i])
    }
}
var嵌入=[{type:“mp3”,url:http://example.com/song.mp3,艺术家:“艺术家”},
{键入:“mp3”,url::http://anotherexample.com/song.mp3,艺术家:“艺术家2”},
{类型:“soundcloud”,url::http://soundcloud.com/summergirlfriends/shockwaves"}];

对于(var i=0;i“按正确顺序”是什么意思?我们谈论的是什么“列表”?如果你发布代码,它会很有帮助。你已经尝试了什么?我们能看到任何代码吗?按正确顺序”是什么意思?我们谈论的是什么“列表”?如果你发布代码,它会很有帮助。你已经尝试了什么?我们能看到任何代码吗?