jquery - Javascript - How next line wait to be executed after asyncronus function is done? -


i'm using filetransfer.download function phonegap plugin download files in app mobile.

this function assyncronus, next lines executed without dependence of conclusion of function.

my app app capture user photo server during login process, user can't moved home screen after action concluded. actual code, doesn't happen, because don't know how it.

below can check code:

var usuarios = json.usuarios;  var filetransfer = new filetransfer();  for(var key in usuarios) {     if(usuarios.hasownproperty(key)){         if(usuarios[key].tipo == "titular"){              var titular = {                 "id"                : usuarios[key].id,                 "email"             : $("#login-email").val(),                 "foto"              : "images/perfil.jpg"             };              localstorage.setitem('appnowa-titular', json.stringify(titular));              if(usuarios[key].foto == "1"){                  window.filedownloadname = "appnowa-perfil-" + usuarios[key].id + ".jpg";                  console.log('downloadfile');                  window.requestfilesystem(                     localfilesystem.persistent,                     0,                     function(filesystem){                         console.log('onrequestfilesystemsuccess');                         filesystem.root.getfile(                             'dummy.html',                             {create: true, exclusive: false},                             function(fileentry){                                 console.log('ongetfilesuccess!');                                 var path = fileentry.tourl().replace('dummy.html', '');                                 var filetransfer = new filetransfer();                                 fileentry.remove();                                  filetransfer.download(                                     'https://www.myserver.com/imagens/clientes/' + window.filedownloadname,                                     path + window.filedownloadname,                                     function(file) {                                         console.log('download complete: ' + file.tourl());me;                                         titular.foto = file.tourl();                                     },                                     function(error) {                                         console.log('download error source ' + error.source);                                         console.log('download error target ' + error.target);                                         console.log('upload error code: ' + error.code);                                         titular.foto = "images/perfil.jpg";                                     }                                 );                               },                             fail                         );                     },                     fail                 );                  console.log("1 " + titular.foto);             }         }     } }  localstorage.setitem('appnowa-dependentes', json.stringify(dependentes));  appnowa.pushpage('mainpage.html'); //go next page 

i think can achieve 2 ways,

chained callbacks or state test in callbacks;

i write pseudo code think working solution (not tested, no guarantee)

aftercharge=function(){   doyourstuffhere; }  //method 1 in callback var complete=0 len = usuarios.lenght; for(var key in usuarios){   //do yourstuff   filetransfer.download(       file,       path,       function(file){         //do stuff         complete++;         if(len===complete){           aftercharge();         }       },       function(error){         //do stuff         complete++;         if(len===complete){           aftercharge();         }       }   ); } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -