简体   繁体   English

如何反复调用回调function?

[英]how to call a callback function repeatedly?

getData is a function that returns data from a third-party source. getData 是从第三方源返回数据的 function。 May return errors.可能返回错误。 key - a string to be passed to the callback function. key - 要传递给回调 function 的字符串。 maxRequestsNumber - max. maxRequestsNumber - 最大值。 the number of calls to the callback function.对回调 function 的调用次数。 If this parameter is absent - repeat an infinite number of times.如果此参数不存在 - 重复无限次。 The getRepeatableData function should call getData and handle errors. getRepeatableData function 应该调用 getData 并处理错误。 If the getData call returns a NotFoundError, then we throw an exception.如果 getData 调用返回 NotFoundError,那么我们将抛出异常。 - If the call to getData returns a TemporaryError error, then we must make a repeated call to the callback function. - 如果调用 getData 返回 TemporaryError 错误,那么我们必须重复调用回调 function。 The number of such calls should not exceed the value of maxRequestsNumber.此类调用的数量不应超过 maxRequestsNumber 的值。 If the number of repeated calls exceeds maxRequestsNumber, then the getRepeatableData function should throw an AttemtsLimitExceeded error.如果重复调用的次数超过 maxRequestsNumber,那么 getRepeatableData function 应该抛出 AttemtsLimitExceeded 错误。

If getData is executed without errors, the function should return what getData returned.如果 getData 执行没有错误,function 应该返回 getData 返回的内容。

function getRepeatableData(getData, key, maxRequestsNumber) {
  try{
    const func = getData();
  }
  catch(e){
    if(e NotFoundError){
      throw e;
    }
    let number = 1;
    if(e == TemporaryError){
      const callFunc = function getData(key){
        if(number > maxRequestsNumber){
          return key;
        }
        return getData(maxRequestsNumber - 1);
      }
    }
    if (callFunc > maxRequestsNumber){
      throw AttemptsLimitExceeded;
    }
  }
  return getData();
}

If I'm understanding the question correctly, you're looking for something like this:如果我正确理解了这个问题,那么您正在寻找这样的东西:

function foo(callback, maxStack) {
    let data = null;
    if (maxStack === 0) {
        throw AttemptsLimitError
    }
    try {
        data = callback();
    } catch(e) {
        if (e == UnrecoverableError) {
            throw e;
        else if (e == RecoverableError) {
            return foo(callback, maxStack === null ? null : maxStack - 1);
        }
    }
    return data;
}

The details are a little different but the concept is the same.细节有点不同,但概念是一样的。 Recursively try again and again until it either works or you reach the attempt limit.递归地一次又一次地尝试,直到它起作用或达到尝试限制。

Solved using recursion, please have a look用递归解决,请看

function getRepeatableData(getData, key, maxRequestsNumber) {
  let output;
  if (!maxRequestsNumber)
    throw AttemptsLimitExceeded;
  try{
    output = getData();
  }
  catch(e){
    if(e instanceof NotFoundError)
      throw e;
    else if(e instanceof TemporaryError)
      getRepeatableData(getData, key, maxRequestsNumber--)
  }
  return output
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM