Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript—异步函数完成后,下一行如何等待执行?_Javascript_Jquery_Asynchronous - Fatal编程技术网

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

Javascript—异步函数完成后,下一行如何等待执行?,javascript,jquery,asynchronous,Javascript,Jquery,Asynchronous,我正在使用Phonegap插件的FileTransfer.download功能在我的移动应用程序中下载文件 此函数是assyncronus,因此执行下一行时不依赖于此函数的结论 我的应用程序在登录过程中从服务器捕获用户照片,因此此操作结束后,用户无法移动到主屏幕。但在我的实际代码中,这并没有发生,因为我不知道怎么做 您可以在下面查看我的代码: var usuarios = json.usuarios; var fileTransfer = new FileTransfer(); for(va

我正在使用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

我认为你可以通过两种方式来实现

链接回调或在所有回调中使用状态测试

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


将异步调用后需要执行的代码放入与异步处理结束相关联的回调函数中。我认为这不会起作用,因为这段代码执行了1次以上(取决于与记录的用户相关联的用户数量)。例如,我有10个从属用户,因此此代码将执行10次以捕获他们的foto。异步回调的结果只能在回调本身中使用。Javascript不会像您希望的那样等待执行。您必须针对异步行为进行设计。
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();
        }
      }
  );
}