3

我正在尝试使用 promise-circuitbreaker 节点模块。我可以将参数传递给被调用函数,但是,我无法让被调用函数返回值。此外,我不断收到我不明白的超时。我显然遗漏了一些东西,但是我在文档中找不到任何解决方案(http://pablolb.github.io/promise-circuitbreaker/

我制作了一个非常简单的示例应用程序来展示我的困难:

var CircuitBreaker = require('promise-circuitbreaker');
var TimeoutError = CircuitBreaker.TimeoutError;
var OpenCircuitError = CircuitBreaker.OpenCircuitError;


function testFcn(input, err) {
  console.log('Received param: ', input);
  //err('This is an error callback');

  return 'Value to return';
}

var cb = new CircuitBreaker(testFcn);

var circuitBreakerPromise = cb.exec('This param is passed to the function');

circuitBreakerPromise.then(function (response) {
  console.log('Never reach here:', response);
})
  .catch(TimeoutError, function (error) {
    console.log('Handle timeout here: ', error);
  })
  .catch(OpenCircuitError, function (error) {
    console.log('Handle open circuit error here');
  })
  .catch(function (error) {
    console.log('Handle any error here:', error);
  })
  .finally(function () {
    console.log('Finally always called');
    cb.stopEvents();
  });

我从中得到的输出是:

Received param:  This param is passed to the function
Handle timeout here:  { [TimeoutError: Timed out after 3000 ms] name: 'TimeoutError', message: 'Timed out after 3000 ms' }
Finally always called

就我而言,我希望返回一个简单的字符串。我不想要超时错误。

如果我取消注释 testFcn() 中的 //err('This is an error callback') 行,我会得到以下输出:

Received param:  This param is passed to the function
Handle any error here: This is an error callback
Finally always called

因此,被调用函数中的第二个参数似乎用于错误处理。

任何帮助将不胜感激。

4

0 回答 0