0

Lets say I solve a common ajax problem (looping ajax and solving the async problem{not sure if this is a problem}). Like this.

var users = [{username: 'foo', email : 'foo@email'}, {username: 'bar', email : 'bar@email'},]

function iterate() {
     log(users[i].username, users[i].email, function(){
         i++;
         if(i < users.length) {
            iterate();
         }
     })
}

function log(usr, email, callback) {
   $.post('/url/', {user: usr, email : email}, function(html){
        if(html.indexOf('success') > 1) {
            set(usr, email, callback);
        }
   });
}

function set(usr,email, callback) {
    $.post(......., function(re){
        if(#validations) {
            next(usr, email, callback);
        }
   });
}

function next(u,e,c){
   $.post(......., function(re){
        if(#validations) {
            c();
        }
   });
}

I read about lazy definition pattern, can this be improved using that pattern? What should I do to be able to do the same thing faster?

Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83

0 Answers0