求解关于promise的一道面试题?

大麦饼
  • 4
新手上路,请多包涵

题目:
实现一个retry函数,使得该defer函数可以在执行5次以后,输出成功与否

const defer = num => new Promise(resolve => setTimeout(() => { console.log(`defer函数:${num}`); resolve() }, 1000));

retry(defer, 5).then(() => { console.log('success') }, () => { console.log('reject') })

我目前的实现:

function retry(func, count) {
  const index = count - 1
  console.log(`retry函数:${index}`)
  if (count === 0) {
    return Promise.resolve()
  }
  const next = () => retry(func.bind(null, index), index)
  return func(index).then(next)
}

请问为什么在能打印如下,defer函数中的值为什么一直都是4,而不是递减到0 ?
另外,请问我的实现是否正确呢?

retry函数:4
defer函数:4
retry函数:3
defer函数:4
retry函数:2
defer函数:4
retry函数:1
defer函数:4
retry函数:0
defer函数:4
retry函数:-1
success
回复
阅读 530
6 个回答
function testA(number) {
   console.log(number);
}

testA(-1) // -1
const testB = testA.bind(null, 5);
testB(-2) // 5
const testC = testB.bind(null, 4);
testC(-3) // 5
const testD = testC.bind(null, 3);
testD(-4) // 5

因为 bind 过后参数就绑定了,再 bind 并不会改变绑定的值

const defer = (num) =>
  new Promise((resolve, reject) =>
    setTimeout(() => {
      console.log(`defer函数:${num}`);
      const value = Math.random();
      return value > 0.5 ? resolve(value) : reject(value);
    }, 1000)
  );

async function retry(fn, count) {
  try {
    console.log(`retry函数:${count}`);
    const result = await fn(count);
    return result;
  } catch (e) {
    if (count <= 0) throw e;
    return retry(fn, count - 1);
  }
}

retry(defer, 5).then(
  (v) => {
    console.log("success", v);
  },
  (e) => {
    console.log("reject", e);
  }
);
const defer = num => new Promise(resolve => setTimeout(() => { console.log(`defer函数:${num}`); resolve() }, 1000));

            function retry(fun, num) {
                if (num == 0){
                    return Promise.resolve();
                }
                return fun(num).then(() => {
                    return retry(fun, --num)
                })
            }

            retry(defer, 5).then(() => { console.log('success') }, () => { console.log('reject') })

这样?

llfididi
  • 5
新手上路,请多包涵
// 实现一个retry函数,使得该defer函数可以在执行5次以后,输出成功与否
const defer = num => new Promise(resolve => setTimeout(() => { console.log(`defer函数:${num}`); resolve() }, 1000));
async function retry(deferFun, num) {
  await deferFun(num) // 等待函数执行完
  --num // 执行完后数字减1
  if (num <= 0) {
    return // 见底就退出
  } else {
    return retry(deferFun, num) // 没见底继续打印
  }
}
retry(defer, 5).then(() => { console.log('success') }, () => { console.log('reject') })
const retry = (handler, count) => {
    var sequence = Promise.resolve();
    for(let i=0; i<count; i++) {
        sequence = sequence.then(() => handler(i));
    }
    return sequence;
}
const retry = async (fun, num) => {
    while (num) {
        await fun(num);
        num--;
    }
};
星期六的猫
  • 3
新手上路,请多包涵
const defer = num => new Promise(resolve => setTimeout(() => { console.log(`defer函数:${num}`); resolve() }, 100));
 async function retry(func, count) {
     try{
           for(let i=count;i>0;i--){
              let res=await func(i)
            
            }
          return Promise.resolve()
     }
     catch(err){
         console.log(err)
          return Promise.reject()
     }

}
retry(defer, 5).then(() => { console.log('success') }, () => { console.log('reject') })
你知道吗?

宣传栏