代码之家  ›  专栏  ›  技术社区  ›  Omar

在继续之前,请等待每个$的异步函数

  •  2
  • Omar  · 技术社区  · 7 年前

    我做了 this fiddle to help debug 以下代码。。。

    1. 我怎样才能确保 year 钥匙/val打开 items[] 每次都不一致。

    2. 我不想改变 titles transformData() . 现在数组正在被修改。。。我的尝试是 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); }
                      }
                  }
              });
          });
      }
      
    1 回复  |  直到 7 年前
        1
  •  3
  •   Anurag Awasthi    7 年前

    我已经更新了你的 fiddle . 你的原因 titles 正在更新,因为 作为参数传递的数组和数组引用了相同的对象。我不是推同一个对象,而是创建新对象,

    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>