循环承诺,连锁承诺 [英] While loop promise, chain promise
问题描述
我有一个脚本,如果此操作失败,应该尝试将其连接到服务器3次.将用户导航到No Connection page
.
I have a script that should try to connect 3 times to server if this fails navigate the user to the No Connection page
.
我不知道如何尝试3次连接,然后在失败的情况下将用户导航到No Connection page
.
I don't know how to attempt a connection 3 times and then navigate user to the No Connection page
if this fails.
如何使用我的代码来解决此问题.
How can I use my code to achieve this problem.
这是我到目前为止的代码:
Here is the code I have so far:
const fetchModule = require('fetch');
const frameModule = require('ui/frame');
let timeTrig;
const waitPeriod = 7000;
function checkServerStatus() {
if (timeTrig) {
clearTimeout(timeTrig);
timeTrig = null;
}
timeTrig = setTimeout(() => {
frameModule.topmost().navigate('./views/no-connection');
}, waitPeriod);
return fetchModule.fetch(`${randomUrl}auth/ping`)
.then((result) => {
clearTimeout(timeTrig);
if (result && result.ok) {
return true;
}
frameModule.topmost().navigate({
moduleName: views/no-connection,
transition: {
name: 'slide',
},
});
})
.catch(() => {
if (timeTrig) {
clearTimeout(timeTrig);
timeTrig = null;
}
frameModule.topmost().navigate({
moduleName: 'views/no-connection',
transition: {
name: 'slide',
},
});
});
}
感谢您的帮助.
推荐答案
也许是您想要的-不确定要重试代码的哪一部分,但这可能会给您一些想法
Perhaps something like this is what you want - not exactly sure which part of your code should be retried, however this may give you some ideas
const fetchModule = require('fetch');
const frameModule = require('ui/frame');
const waitPeriod = 7000;
// retry "cont" times or until the promise resolves
const retry = (cont, fn) => fn().catch(err => cont > 0 ? retry(cont - 1, fn) : Promise.reject(err));
// failure code - reason will be the last failure reason ('not ok' or 'timeout') - not that it is used in your code
const noConnection = reason => frameModule.topmost().navigate({
moduleName: 'views/no-connection',
transition: {
name: 'slide',
},
});
// lets race between (resolved or rejected) fetch and a rejected timeout
const doFetch = () => Promise.race([
fetchModule.fetch(`${randomUrl}auth/ping`).then(result => {
if (result && result.ok) {
return true;
}
return Promise.reject('not ok');
}),
// above can be written as
// fetchModule.fetch(`${randomUrl}auth/ping`).then(result => (result && result.ok) || Promise.reject('not ok')),
new Promise((resolve, reject) => setTimeout(reject, waitPeriod, 'timeout'))
]);
const checkServerStatus = () => {
retry(3, doFetch).catch(noConnection)
};
或者,这是相同的代码,只是组织方式不同.我觉得更好:p
Alternatively, this is the same code, just organised differently. I think better :p
const fetchModule = require('fetch');
const frameModule = require('ui/frame');
const waitPeriod = 7000;
// retry "cont" times or until the promise resolves
const retry = (cont, fn) => fn().catch(err => cont > 0 ? retry(cont - 1, fn) : Promise.reject(err));
// reject after a given timeout
const delayedReject = (delay, reason) => new Promise((_, reject) => setTimeout(reject, delay, reason));
//
const doFetch = () => fetchModule.fetch(`${randomUrl}auth/ping`)
.then(result => (result && result.ok) || Promise.reject('not ok'));
const checkServerStatus = () => retry(3, () => Promise.race([doFetch(), delayedReject(waitPeriod, 'timeout')]))
.catch(reason => frameModule.topmost().navigate({
moduleName: 'views/no-connection',
transition: {
name: 'slide',
},
}));
这篇关于循环承诺,连锁承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!