Ionic 框架 resolveLocalFileSystemURL 是异步的吗? [英] Is Ionic Framework resolveLocalFileSystemURL Asynchronous?

查看:30
本文介绍了Ionic 框架 resolveLocalFileSystemURL 是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的 AngularJS 控制器调用一个函数.当函数完成执行时,我想打印函数调用后的结果.

I am calling a function from my AngularJS controller. When the function completes execution, I would like to print the result after the function call.

此代码在控制器内部,称为 on-tap

This code is inside the controller, which is called on-tap

  //count the number of files inside a directory
   var count = countDir(cordova.file.dataDirectory +'/Images/');
   console.log('Hello ');
   console.log('Count of directory '+count);

这里是 countDir 函数.此函数查找目录中的文件数,然后返回计数

Here is the countDir function. This function finds the number of files in a directory and then returns the count back

function countDir(path){
          var count = 0;
          console.log('Counting the files inside '+path);
          window.resolveLocalFileSystemURL(path,
            function (fileSystem) {
              var reader = fileSystem.createReader();
              reader.readEntries(
                function (entries) {
                  var i;
                  for (i=0; i<entries.length; i++)  {    
                      console.log('File name ' + entries[i].name + ' added to count');
                       count++;   
                  }
                },
                function (err) {
                  console.log(err);
                }
              );
            }, function (err) {
              console.log(err);
            }
          );
  return count; 
}

我面临的挑战是,我的调用代码首先打印Hello"和目录 0 计数",然后打印 countDir 中的所有内容.

The challenge that I am facing is that, my calling code first prints 'Hello' and 'Count of directory 0' and then everything inside countDir is printed.

countDir() 的调用是异步的吗?如果是这样,我如何确保我的调用代码在 countDir() 返回结果后继续执行.

Is the call to countDir() asynchronous? If so, how can I ensure that my calling code proceeds once countDir() has returned a result.

推荐答案

提供给 window.resolveLocalFileSystemURLreader.readEntries 的匿名函数是异步调用的.处理这个问题的AngularJS 方式"是使用 $q 服务来创建和返回一个承诺.

The anonymous functions furnished to both window.resolveLocalFileSystemURL and reader.readEntries are invoked asynchronously. The "AngularJS way" to handle this is to use the $q service to create and return a promise.

function countDirPromise(path){
  //create $q.defer object
  var q = $q.defer();
  var count = 0;
  console.log('Counting the files inside '+path);
  $window.resolveLocalFileSystemURL(path,
    function onUrlSuccess(fileSystem) {
      var reader = fileSystem.createReader();
      reader.readEntries(
        function onReaderSuccess(entries) {
          var i;
          for (i=0; i<entries.length; i++)  {    
              console.log('File name ' + entries[i].name + ' added to count');
               count++;   
          }
          //resolve with count
          q.resolve(count);
        },
        function onReaderError(err) {             
          console.log(err);
          //reject with error
          q.reject(err);
        }
      );
    }, function onUrlError(err) {
      console.log(err);
      //reject with error
      q.reject(err);
    }
  );
  //return count promise
  return q.promise;
}

如您所见,有四个嵌套函数:onUrlSuccessonUrlErroronReaderSuccessonReaderError.这些函数被异步调用.如果 onUrlSuccess 和嵌套函数 onReaderSuccess 都被调用,promise 将使用值 count 解析.如果 onUrlError 函数或 onReaderError 函数被调用,则承诺会因错误而被拒绝.

As you can see, there are four nested function: onUrlSuccess, onUrlError, onReaderSuccess, and onReaderError. Those functions get invoked asynchronously. The promise resolves with the value count if both onUrlSuccess and the nested function onReaderSuccess get invoked. The promise is rejected with an error if either the onUrlError function or onReaderError function get invoked.

var countPromise = countDirPromise(cordova.file.dataDirectory +'/Images/');
console.log('Hello ');
countPromise.then(function onSuccess(count) {
    console.log('Count of directory '+count);
    //return to chain data
    return count;
}).catch(function onReject(err) {
    console.log(err);
    //throw to chain rejection
    throw err;
}).then(anotherSuccessFn, anotherRejectFn);

提供给 .then.catch 方法的函数由 $q 服务异步调用.

The functions furnished to both the .then and .catch methods are invoked asynchronously by the $q service.

有关更多信息,请参阅AngularJS $q 服务 API 参考 -- 延迟 API

For more information, see AngularJS $q Service API Reference -- The Deferred API

另一种方法是使用 返回的 $q 服务承诺ngCordova $cordovaFile API.

The alternative approach is to use the $q service promises returned by the ngCordova $cordovaFile API.

function countDirPromise(path){
  var count = 0;
  console.log('Counting the files inside '+path);
  var promise = $cordovaFile.checkDir(path);
  var derivedPromise = promise.then(
    function onSuccess(fileSystem) {
      var q = $q.defer()
      var reader = fileSystem.createReader();
      reader.readEntries(
        function onReaderSuccess(entries) {
          var i;
          for (i=0; i<entries.length; i++)  {    
              console.log('File name ' + entries[i].name + ' added to count');
               count++;   
          }
          //resolve with count
          q.resolve(count);
        },
        function onReaderError(err) {             
          console.log(err);
          //reject with error
          q.reject(err);
        }
      );
      //return to chain promise
      return q.promise;
    };
  });
  return derivedPromise;
}

这篇关于Ionic 框架 resolveLocalFileSystemURL 是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆