WinJS:在 512MB WinPhone8.1 Emu 上使用 BackgroundUploader class 后,Promise 链有时会卡住

WinJS: Promise chain sometimes sticks after usage of BackgroundUploader class on 512MB WinPhone8.1 Emu

我在 Windows Phone 8.1 512MB RAM 模拟器上的 WinJS 应用程序有时会出现奇怪的错误。我无法在其他模拟器实例或设备上重现它。

执行贯穿承诺链并完成以下 return 语句:

  return ( currentUpload = uploadOperation.startAsync() );

在那之后什么也没有发生。 我在赋予 .then 定义的所有三个函数(成功、失败、挂起)处设置了断点。 None这三个功能代码在出现这种奇怪情况时将永远到达

我也把这个 return 语句放在 try catch 块上,但没有捕获异常。

代码的简短说明:

查看完整代码:

var currentUpload;  // global

 function uploadFile(localFullPath, headers, serverUrl)
 {
    var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath),
        uploader = false;

        try
        {
            uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
            uploader.method = 'PUT';

            // set headers to uploader
            for (var key in headers)
            {
                if (headers.hasOwnProperty(key))
                    uploader.setRequestHeader(key, headers[key]);
            }
        }
        catch (e)
        {
            // error handling
            return false;
        }

        Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri)
        .then(function success(file)
        {
            return uploader.createUpload(serverUrl, file);
        },
        function failure(error)
        {
            return WinJS.Promise.wrapError('file not found');
        })

        .then(function (uploadOperation)
        {
            if (currentUpload == 'Canceled')
                return WinJS.Promise.wrapError('upload canceled');
            else
                return ( currentUpload = uploadOperation.startAsync() );

        })

        .then(function success(success)
        {
            currentUpload = false;
            // success handling
            return true;
        }, function failure(error)
        {
            currentUpload = false;
            // error handling
            return false;
        }

        }, function pending(status)
        {
            var progress = status.progress,
                percent = Math.round(progress.bytesSent / progress.totalBytesToSend * 100);

            // progress handling
        });
        }

感谢您的帮助!

P.S。尽管我没有在 BackgroundUploader class:

上使用 group/TransferGroup,但我也收到了已弃用的警告

The method Windows.Networking.BackgroundTransfer.IBackgroundTransferBase.put_Group has been deprecated. Group may be altered or unavailable for releases after Windows 8.1. Instead, use TransferGroup.

可能与那个 promise 链错误有关。

可能是一个简单的拼写错误??
只需检查下面代码s中的注释

var currentUpload;  // global

 function uploadFile(localFullPath, headers, serverUrl)
 {
    var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath),
        uploader = false;

        try
        {
            uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
            uploader.method = 'PUT';

            // set headers to uploader
            for (var key in headers)
            {
                if (headers.hasOwnProperty(key))
                    uploader.setRequestHeader(key, headers[key]);
            }
        }
        catch (e)
        {
            // error handling
            return false;
        }

        /*

         return something it's a good practice if you want to chain Promises
           |
           |
           V                                                                       */  
        return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri)
        .then(
          function success(file){
            return uploader.createUpload(serverUrl, file);
          },
          function failure(error){
            return WinJS.Promise.wrapError('file not found');
  //                                        |_____________|  
  // your are rejecting your promise with ---------^
          }
        ).then(
          function (uploadOperation){

  // Just avoid this type of code construction
  // and add brace !
  // adding brace augment readability and prevent errors !

           // where do you set this ??? currentUpload = 'Canceled'
           // is part of winjs ???
           if (currentUpload == 'Canceled') 

           // why not handle your rejected promise ?
           // by something like :
           //if( uploadOperation == 'file not found' )
                return WinJS.Promise.wrapError('upload canceled');
            else
                return ( currentUpload = uploadOperation.startAsync() );
          }
        ).then(
// Promise resolve handler
          function success(success){
            currentUpload = false;
            // success handling
            return true;
          } , 
// Promise reject handler
          function failure(error){
            currentUpload = false;
            // error handling
            return false;
          }
/*        ^  
          |
YOU HAVE HERE A DOUBLE CLOSING BRACE }} ???
 °
 |  WHAT IS THIS PART FOR ??????
 |
 +------+---- Ending uploadFile with successive closing brace !
        |
        |+---------------- Declaration separator
        ||      +--- New function declaration
        ||      |
        VV      V                     */
        }, function pending(status){
            var progress = status.progress,
                percent = Math.round(progress.bytesSent / progress.totalBytesToSend * 100);
            // progress handling
        });
        }

/*
ALL THAT PART ABOVE IS NOT IN THE PROMISE CHAIN 
AND SHOULD BREAK YOUR CODE !!!
HOW HAVE YOU EVER COULD RUN THIS ???
*/

为了确定这一点,它可能遗漏了您的代码的一部分,因为在这种状态下,您的代码必须中断!


也许调整可以是:

var currentUpload;  // global

function uploadFile(localFullPath, headers, serverUrl)
{
  var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath),
      uploader = false;

  try
  {
    uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
    uploader.method = 'PUT';

    // set headers to uploader
    for (var key in headers)
    {
      if (headers.hasOwnProperty(key))
        uploader.setRequestHeader(key, headers[key]);
    }
  }
  catch (e)
  {
    // error handling
    return false;
  }

  /*

  return something it's a good practice if you want to chain Promises
    |
    |
    V                                                                     */  
  return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri)
  .then(
    function success(file){
      return uploader.createUpload(serverUrl, file);
    },
    function failure(error){
      return WinJS.Promise.wrapError('file not found');// O|--------+
    }//                                                             |
  ).then(//                                                         |
    function (uploadOperation){//  O|-------------------------------|
      //                                                            |
      // Just avoid this type of code construction                  |
      // and add brace !                                            |
      // adding brace augment readability and prevent errors !      |
      //                                                            |   
      // EDIT                                                       |
      //if (currentUpload == 'Canceled') { // <--- add brace        |
      if (uploadOperation == 'file not found') { //<---add brace <--+ 

        return WinJS.Promise.wrapError('upload canceled');

      } else { // <---- add braces

        // even if it is valid
        // avoid assignement like that
        // so subtil to read / understand / debug ...
        // return ( currentUpload = uploadOperation.startAsync() );

        currentUpload = uploadOperation.startAsync();
        // maybe debug here ?
        console.log('currentUpload : ' + currentUpload );
        return currentUpload;

      } // <---- add brace
    }
  ).then(
    function success(success){
      currentUpload = false;
      // success handling
      return true;
    } , 
    function failure(error){
      currentUpload = false;
      // error handling
      return false;
    } ,
    function pending(status){
      var progress = status.progress,
          percent = Math.round(progress.bytesSent / progress.totalBytesToSend * 100);
      // progress handling
    }
  ).done( // it is always a good thing to have a final catcher !!
    function(){
      // things are ok !
      console.log('ok');
    },
    function(err){
      // make something with your error better than
      alert(err);
      console.log('ko');

    }
  );
}

编辑

进一步查看(我不使用winjs),有一些事情需要澄清:

.then(
  function success(file) {
    return uploader.createUpload(serverUrl, file);
  },
  function failure(error) {
    return WinJS.Promise.wrapError('file not found');

  }
).then(
  function(uploadOperation) {

    if (currentUpload == 'Canceled')
      return WinJS.Promise.wrapError('upload canceled');
    else
      return (currentUpload = uploadOperation.startAsync());
  }
).

你在哪里设置这个:
currentUpload = 'Canceled'
您正在检查,但我很确定这部分代码永远不会到达。
在此之前你拒绝你的承诺:
return WinJS.Promise.wrapError('file not found');
也许下一个 then 应该处理这样的事情:

.then(
  function success(file) {
    return uploader.createUpload(serverUrl, file);
  },
  function failure(error) {
    return WinJS.Promise.wrapError('file not found');

  }
).then(
  function(uploadOperation) {

    if (uploadOperation == 'file not found')
      return WinJS.Promise.wrapError('upload canceled');
    else
      return (currentUpload = uploadOperation.startAsync());
  }
)

希望对您有所帮助。