javascript - Looping ajax calls using deferred and promises -
i new jquery $.deferred
, seems cannot understand whole bunch of stuffs regarding async processing.
let wanna loop through usernames , login them, process something, , logout(this might not necessary include in question).
if have 2 user/pass combinations, want loop through them, login them , necessary actions.
[{"username":"user1","password":"hardtoknow"}, {"username":"user2","password":"hardtoknow"}]
and have created function based on read, although not on how works.
function login(user, pass) { var d = $.deferred(); $.post( 'http://www.domain.com/login', { user: user, passwrd: pass } ).done(function(s){ d.resolve(s); }).fail(d.reject); return d.promise(); }
the above function login, , have function must have async request.
function settodo(user,pass) { return login(user,pass) .pipe(function(s){ if (s.indexof('<li class="greeting">hello <span>' + user + '</span></li>')) { return $.post().....? } return set(usr, pw); }); }
that function above, although not complete, idea, try set todo , later function called submit or finalize todo
function submit(user, pass) { return settodo(user, pass) .pipe(function(s){ // $.post() here... } }
question, doing correctly? deferred? how loop through login credentials? tried using $.each , ajax request them, expected executed @ same time because async.
yes. overall doing right, note pipe
being deprecated in favor of then
.
Comments
Post a Comment