Javascript - 异步函数完成后下一行如何等待执行

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

本文关键字:一行 何等待 执行 等待 函数 Javascript 异步      更新时间:2023-09-26

我正在使用Phonegap插件中的FileTransfer.download功能在我的应用程序移动设备中下载文件。

此函数

是 assyncronus,因此执行下一行时不依赖于此函数的结论。

我的应用应用在登录过程中从服务器捕获用户照片,因此在此操作结束后无法将用户移动到主屏幕。但是对于我的实际代码,它不会发生,因为我不知道该怎么做。

您可以在下面检查我的代码:

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 to next page

我认为您可以通过两种方式实现它,

链式回调或在所有回调中进行状态测试;

我将编写我认为可能是工作解决方案的伪代码(未经测试,不保证)

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