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

I have a strange error that sometimes occurs in my WinJS application on the RAM emulator for Windows Phone 8.1 512MB. I could not play it on other emulator instances or on the device.

Execution is done through a promise chain and completes the following return statement:

return ( currentUpload = uploadOperation.startAsync() ); 

After that, nothing happens. I set breakpoints on all three functions (success, failure, pending expectations) that are given to the .then definition. None of these three functional codes will ever be reached when this strange case occurs.

I also put this return statement in a catch try block, but there was no exception for catch.

short code description:

  • Background loader instance created (custom headers + PUT method)

  • StorageFile opens with URI

  • The background loader prepares to download this file (definition of uploadOperation)

  • uploadOperation will be launched

see full code:

 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 }); } 

Thanks for any help!

PS I also received an obsolete warning, although I do not use the / TransferGroup in the BackgroundUploader class:

The method Windows.Networking.BackgroundTransfer.IBackgroundTransferBase.put_Group is deprecated. The group may be modified or unavailable for releases after Windows 8.1. Use TransferGroup instead.

Perhaps this is due to this promise chain error.

+5
source share
1 answer

Maybe a simple typo mistake?
Just check the comments in the codes below

 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 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 ??? */ 

maybe he missed part of your code to be sure of it, because in this state your code should break!


Perhaps the setting could be:

 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 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'); } ); } 

EDIT

Looking further (I do not use winjs), I need to clarify something:

 .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()); } ). 

where do you install this:
currentUpload = 'Canceled'
You check it out, but I'm sure this piece of code will never be reached.
Before that, you reject your promise: return WinJS.Promise.wrapError('file not found');
perhaps the next then should handle the following:

 .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()); } ) 

Hope this helps you.

+2
source

Source: https://habr.com/ru/post/1234095/


All Articles