• jquery deferred in .each loop


    https://stackoverflow.com/questions/20688803/jquery-deferred-in-each-loop

    Asked 7 years, 3 months ago
    Viewed 26k times
    27

    This should be a simple one. I have a function that is called and I need to wait for all the async operations to complete. what I want is something like this...

    self.processSchema(data).done(function(results){ //do stuff});
    

    The processSchema function loops using $.each and calls an async method.

    var processSchema = function(data)
    {
         var def = new $.Deferred();
         $.each(table, function()
         {
             //calls an async SQLitePlugin method
             db.executeSql(sql, data, function(tx, results){
                def.resolve(results);
             }
         }
    
         return(def.promise());
    }
    

    This does not seem to work, I am new to $.Deferred so any guidance would be helpful

     
    •  
      I think you would need a new deferred for every single db.executeSql, and then do a large $.when(deferred1, deferred2...defferedN).then(function(data1, data2...dataN) { }); – Robin Giltner Dec 19 '13 at 18:17

    2 Answers

    36
     

    You'll need a promise for each iteration

    var processSchema = function(data) {
         var promises = [];
    
         $.each(table, function() {
             var def = new $.Deferred();
             db.executeSql(sql, data, function(tx, results){
                def.resolve(results);
             });
             promises.push(def);
         });
    
         return $.when.apply(undefined, promises).promise();
    }
    
     
    •  
      adeneo, should that return defcon.promise() ? – gdex Dec 19 '13 at 18:24
    • 1
      Yes it should! One could return the array of promises, but to keep the syntax of how the OP want's to call the function, I figure one master promise that is returned and then resolved when all the other promises are resolved would be easier, and then pass all the returned data as the arguments array directly ? – adeneo Dec 19 '13 at 18:28 
    •  
      @Jan Dvorak, so the def.apply will work inside of loop? – gdex Dec 19 '13 at 18:31
    • 2
      @adeneo actually you can simply return result of $.when, without creating "master" promise? – hawk Dec 19 '13 at 18:35
    • 2
      @Tomalak $.when.apply(...).promise() – John Dvorak Dec 19 '13 at 18:39
    8

    For Functional Programming fiends (like myself), here's a single-expression version of adeneo's answer:

    var processSchema = function(data) {
        return $.when.apply($, $.map(table, function() {
            var def = new $.Deferred();
            db.executeSql(sql, data, function(tx, results){
                def.resolve(results);
            });
            return def;
        })).promise();
    };
    

    Also I'd like to note that you are iterating over table, but aren't doing anything with each item in the iteration (i.e. the callback in your each has no arguments.) Now, I'm not sure what your goal is, but this doesn't seem right to me :P

    What Doesn't Kill Me Makes Me Stronger
  • 相关阅读:
    geowebcache发布 arcgis 瓦片
    BLANK
    基于 SpringBoot 高仿某度网盘项目,前后端分离(含源码)
    基于SpringBoot+WebMagic实现一个的爬虫框架
    博客园主题
    vue el 自动计算时间加1天
    python报警告qt.gtimg.cn
    量化交易日志
    mybatis 一对多。对多对
    DBeaver执行SQL脚本,导入导出
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/14599753.html
Copyright © 2020-2023  润新知