简体   繁体   English

等待$ .each的异步功能,然后继续

[英]wait for async function with $.each before continuing

I made this fiddle to help debug the following code... 我做了这个小提琴来帮助调试以下代码...

If you go to the fiddle and keep hitting "run" - you will notice that the results are different every time (they should be the same). 如果您去找小提琴并继续按“ run”(运行)-您会注意到每次的结果都是不同的(它们应该是相同的)。 I am trying to accomplish 2 thing 我正在尝试完成两件事

  1. how can i make sure that the year key/val is on items[] every time — it is not consistent now. 我如何确保year键/值每次都在items[] -现在不一致。

  2. I do not want to alter titles during transformData() . 我不想在transformData()期间更改titles As it is now the array is being modified... my attempt was transformData(titles.slice(0), items => { but it is still being altered. 由于现在数组正在被修改...我的尝试是transformData(titles.slice(0), items => {但它仍在被修改。

     const titles = [ { title: 'avatar' }, { title: 'jurassic' }, { title: 'black panther' } ]; transformData(titles.slice(0), items => { const problem = items.length !== titles.length; const debugg ={ problem: problem, counts: { items: items.length, // echos 2 titles: titles.length // echos 3 }, items: items, // echos an object with 3 arrays inspector shows length:3 titles: titles // echos an object with 3 arrays inspector shows length:3 }; console.log('debugg', debugg) $('pre').text(JSON.stringify(debugg, null, 2)) }); function transformData(configs, next) { const self = this; const items = []; const last = configs.length; $.each(configs, function(i, config) { items.push(config); $.ajax({ url: 'https://www.omdbapi.com/?apikey=f4e09aec&&t=' + items[i].title, type: 'GET', crossDomain: true, dataType: 'jsonp', success: function(results) { if (results.Response != 'False') { console.log(results); items[i].year = results.Year; if (i+1 === last) { next(items); } } } }); }); } 

I have updated your fiddle . 我已经更新了你的小提琴 The reason your titles was getting updated, because titles array and array passed as arguments had reference to same objects. titles得到更新的原因,因为titles数组和作为参数传递的数组都引用了相同的对象。 Instead of pushing same object, i'm creating new objects, 我创建了新的对象,而不是推送相同的对象,

items.push({...config})

 const titles = [ { title: 'avatar' }, { title: 'jurassic' }, { title: 'black panther' } ]; transformData(titles.slice(0), items => { const problem = items.length !== titles.length; const debugg ={ problem: problem, counts: { items: items.length, // echos 2 titles: titles.length // echos 3 }, items: items, // echos an object with 3 arrays inspector shows length:3 titles: titles // echos an object with 3 arrays inspector shows length:3 }; console.log('debugg', debugg) $('pre').text(JSON.stringify(debugg, null, 2)) }); function transformData(configs, next) { const self = this; const items = []; const last = configs.length; const p = [] $.each(configs, function(i, config){ items.push({...config}) p.push($.ajax({ url: 'https://www.omdbapi.com/?apikey=f4e09aec&&t=' + items[i].title, type: 'GET', crossDomain: true, dataType: 'jsonp' })) }) Promise.all(p).then((values)=>{ for(var i =0;i<values.length;i++){ items[i].year = values[i].Year } next(items) }) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <pre></pre> 

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

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