gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:30:36 26 4
gpt4 key购买 nike

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

执行遍历 promise 链并完成以下 return 语句:

  return ( currentUpload = uploadOperation.startAsync() );

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

我也将此 return 语句放在 try catch block 中,但没有捕获异常。

代码简要说明:

  • Background Uploader 实例已创建(自定义 header + PUT 方法)

  • StorageFile通过URI打开

  • 后台 uploader 准备上传该文件(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
});
}

感谢您的帮助!

附言尽管我没有在 BackgroundUploader 类上使用 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 = '已取消'
您正在检查,但我很确定这部分代码永远不会到达。
在此之前你拒绝你的 promise :
返回 WinJS.Promise.wrapError('找不到文件');
也许下一个 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());
}
)

希望对您有所帮助。

关于javascript - WinJS:在 512MB WinPhone8.1 Emu 上使用 BackgroundUploader 类后,Promise 链有时会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33232043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com