WinJS:在512MB WinPhone8.1 Emu上使用BackgroundUploader类后,Promise链有时会出现问题

时间:2015-10-20 08:49:26

标签: javascript windows-phone-8.1 promise winjs

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

执行通过promise链运行并完成以下return语句:

  return ( currentUpload = uploadOperation.startAsync() );

之后没有任何反应。 我在.then定义的所有三个函数(成功,失败,待定)中设置了断点。当出现这种奇怪的情况时,这三个功能代码都不会到达。

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

代码的简短说明:

  • 后台创建了上传器实例(自定义标头+ PUT方法)

  • StorageFile由URI

  • 打开
  • 后台上传程序准备上传该文件(uploadOperation的定义)

  • 将启动uploadOperation

查看完整代码:

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类上使用group / TransferGroup:

  

方法Windows.Networking.BackgroundTransfer.IBackgroundTransferBase.put_Group   已被弃用。组可能被更改或不可用于发布   在Windows 8.1之后。相反,请使用TransferGroup。

可能与承诺链错误有关。

1 个答案:

答案 0 :(得分:2)

也许一个简单的拼写错误?
只需查看下面代码 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());
  }
)

我希望这会对你有所帮助。