Last active
July 10, 2017 15:05
-
-
Save elrrrrrrr/406c9fd6e03374579fbb to your computer and use it in GitHub Desktop.
es6 promise retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// 重试间隔 | |
var INTERVAL = 1000; | |
// mock ajax data 只作演示 | |
var success = false; | |
/** | |
* keepTry | |
* | |
* @name keepTry | |
* @function | |
* @access public | |
* @param {Array} args 参数列表 | |
* @param {Number} times 重试次数 | |
* @param {fn} p promise 构造函数 | |
*/ | |
function keepTry(args, times, p) { | |
var promise = new Promise(p); | |
console.log('发发发发'); | |
// 发一个请求 | |
if (success) { | |
promise.then((data) => {console.log(data)}); | |
} else if (times > 0) { | |
// mock 第三次 返回成功 | |
if (times === 3) { | |
success = true; | |
} | |
setTimeout(function() { | |
keepTry(args, times - 1, p); | |
}, INTERVAL); | |
} else { | |
promise.catch(function(err) { | |
// TODO | |
console.log(err); | |
}); | |
} | |
} | |
var p = function(resolve, reject) { | |
if (success) { | |
// 假装发了一个 ajax | |
setTimeout(function() { | |
resolve('Here is the data'); | |
}, 500) | |
} else { | |
reject(new Error('Fetch error')); | |
} | |
}; | |
keepTry([], 3, p); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// 重试间隔 | |
var INTERVAL = 1000; | |
// mock 返回值 | |
var mockData = 500; | |
// mock ajax data 只作演示 | |
var success = false; | |
// 重试次数 | |
var times = 5; | |
/** | |
* getFetchDataPromise | |
* 获取发送 ajax 的 promise | |
* 可以把 times 等参数封装进去 | |
* 示例代码暂时不用 | |
* | |
* @name getFetchDataPromise | |
* @function | |
* @access public | |
*/ | |
var getFetchDataPromise = function() { | |
return new Promise(function(resolve, reject) { | |
console.log('我要发发发'); | |
if (success) { | |
setTimeout(function() { | |
resolve(mockData); | |
}, 500); | |
} else { | |
reject(new Error('Fetch error')); | |
} | |
}); | |
}; | |
/** | |
* startFetch | |
* 执行 ajax 请求的主函数 | |
* 递归执行 | |
* | |
* @name startFetch | |
* @function | |
* @access public | |
* @param {String} data mockData 返回值 | |
*/ | |
function startFetch(data) { | |
if (times -- && data !== 500) { | |
getFetchDataPromise() | |
.then(parseData) | |
.catch(startFetch); | |
} else { | |
handlerError(data); | |
} | |
} | |
/** | |
* parseData | |
* 解析返回结果、判断是否成功 | |
* | |
* @name parseData | |
* @function | |
* @access public | |
* @param {String} data mockData 返回值 | |
*/ | |
function parseData(data) { | |
if (data === 500) { | |
handlerError(data); | |
} else { | |
handerSuccess(data); | |
} | |
} | |
/** | |
* handerSuccess | |
* 成功获取数据回调 | |
* | |
* @name handerSuccess | |
* @function | |
* @access public | |
* @param {String} data mockData 返回值 | |
*/ | |
function handerSuccess(data) { | |
console.log('success ', data); | |
} | |
/** | |
* handlerError | |
* 获取数据失败回调 | |
* | |
* @name handlerError | |
* @function | |
* @access public | |
* @param {String} data mockData 返回值 | |
*/ | |
function handlerError(data) { | |
console.log('error', data === 500 ? ' server-error' : ' requests-max'); | |
} | |
// 5次失败的场景 | |
// startFetch(); | |
// 获取 500 | |
// success = true; | |
// startFetch(); | |
// 成功 | |
// success = true; | |
// mockData = 200; | |
// startFetch(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment