高级农民
- 积分
- 2387
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-11-7
- 最后登录
- 1970-1-1
|
按我的理解是这样的:
- // test case
- function f(data, scb, ecb) {
- let success = false
- // let success = true
- if (success) {
- scb(data)
- } else {
- ecb(data)
- }
- }
- function retry(n, data, scb, ecb) {
- let nscb = function () {
- n = 0
- scb()
- }
- f(data, nscb, ecb)
- return n === 0 ? null : retry(n - 1, data, scb, ecb)
- }
- /*
- If sucess, there will be only 1 successs (the only execution)
- If fail, there will be 5 fail (the first execution + four extra executions)
- */
- retry(4, 2, () => console.log('success'), () => console.log('fail'))
复制代码 |
|