# js轮询

# 简版--await

export const sleep = (timeout = 100) => {
  return new Promise(resolve => {
    setTimeout(resolve, timeout)
  })
}

// 轮询函数
const polling = async (request, times) => {
  if (times <= 0) {
    return false;
  }

  const result = await request();
  if (result) {
    return true;
  }

  await sleep(1000);
  return polling(request, times - 1);
};

// 请求主体函数
const checkOrder = async orderSn => {
  const {
    data: { status },
  } = await Axios.get('/mall/order/status', {
    params: { orderSn },
  })
  return status === 2
}

// 调用
// 当请求主体函数返回true或者times跑完时候会终止
const status = await polling(() => checkOrder(appOrderSn), 3)