我有一个脚本,如果失败,应该尝试将用户导航到No Connection page
,则应尝试将用户连接到服务器 3 次。
我不知道如何尝试连接 3 次,然后在失败时将用户导航到No Connection page
。
如何使用我的代码来解决此问题。
这是我到目前为止的代码:
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',
},
});
});
}
任何帮助,不胜感激。
也许这样的东西是你想要的 - 不确定应该重试代码的哪一部分,但这可能会给你一些想法
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
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',
},
}));
这种"尝试n次然后失败"类型的问题经常出现,让我们以http获取或Web套接字的形式出现。我实际上之前已经实现了一个承诺的TryNTimes功能。
这是一个更有趣的版本,我认为它非常优雅且可重复使用。
我们有几个实用程序函数来模拟获取请求和承诺排序。
下面的bring
函数类似于 fetch,解析概率为 50%。
bring = (url,i) => new Promise((v,x) => Math.random() > 0.50 ? v(`Data from ${url}`) : x(`Error @ promise # ${i}`))
接下来,我们设置一个大小为 n 的此类bring
函数数组。
promises = (f,n) => Array(n).fill(f),
接下来,我们对递归返回bring
函数的承诺进行排序。如果调用的 bring
函数返回的承诺解析,我们返回该分辨率,但如果拒绝,则我们继续数组中的下一个,直到没有更多的。显然,数组的大小是尝试计数。
var bring = (url,i) => new Promise((v,x) => Math.random() > 0.50 ? v(`Data from ${url}`) : x(`Error @ promise # ${i}`)),
promises = (f,n) => Array(n).fill(f),
tryProms = ([f,...fs], url, i = 1) => f(url,i).then(v => `${v} @ try # ${i}`,
e => (console.log(`Failed @ try #${i}`),
fs.length ? tryProms(fs,url,++i)
: `All ${i} tries are failed`));
tryProms(promises(bring,3),"http://www.someAPI/data").then(console.log, console.log);