简体   繁体   English

Promises:如何返回具有其他错误处理程序的承诺

[英]Promises: how to return a promise which has a otherwise error handler

I am using tableau js api and calling worksheet.applyFilterAsync & workbook.changeParameterValueAsync methods based on certain conditions.我正在使用 tableau js api 并根据某些条件调用worksheet.applyFilterAsyncworkbook.changeParameterValueAsync方法。 These methods have an otherwise error handler.这些方法具有其他错误处理程序。

I am also in between the executing flow return my custom promise like我也在执行流程之间返回我的自定义承诺

      return new Promise((resolve, reject) => {
        resolve(true)
      });

the issue is when i am returning promise and not changeParameterValueAsync or worksheet.applyFilterAsync i am getting an error问题是当我返回 promise 而不是changeParameterValueAsyncworksheet.applyFilterAsync时,我收到错误

TypeError: undefined is not a function (near '...}).otherwise(function (err) {...')

looks like the promise i return does not have a otherwise error handler?看起来我返回的承诺没有其他错误处理程序?

relevant code is as follows相关代码如下

  const applyStep = (step) => {
    if(step.step_type == 'filter') {
      return worksheet.applyFilterAsync(step.name, values, 'replace');
    } else if(step.step_type == 'parameter') {
      return workbook.changeParameterValueAsync(`${step.name}`, value);
    } else {
      return new Promise((resolve, reject) => {
        resolve(true)
      });
    }
  };


  const applyFilters = (counter) => {
    return new Promise((resolve, reject) => {
      let filter = filters[counter];
      if(filter) {
        applyStep(filter).then(promiseTimeout(filter.delay)).then(() => resolve(applyFilters(++counter))).otherwise(function(err) {
          reject(err);
        });
      } else {
        resolve(true);
      }
    });
  };

I need the otherwise error handler to handle error returned by tableau api methods but it does not work in cases where i am returning my custom promise, may be because the promise i am returning does not have an otherwise error handler?我需要otherwise错误处理程序来处理由 tableau api 方法返回的错误,但在我返回自定义承诺的情况下它不起作用,可能是因为我返回的承诺没有其他错误处理程序?

How can address this any help in fixing this will be really great, Thanks.如何解决这个问题,解决这个问题的任何帮助都会非常好,谢谢。

From the tableau docs :画面文档

otherwise(errback: Function) Promise Registers a rejection handler.否则(errback: Function) Promise 注册一个拒绝处理程序。 Shortcut for then(null, errback). then(null, errback) 的快捷方式。

So in your code, you could do所以在你的代码中,你可以做

applyStep(filter)
.then(promiseTimeout(filter.delay))
.then(() => resolve(applyFilters(++counter)))
.then(null, function(err) {
    reject(err);
});

That is instead of .otherwise(err) you can use .then(null, err) .那不是.otherwise(err)你可以使用.then(null, err)

The issue is that you are currently mixing two different types of promises.问题是您目前正在混合两种不同类型的承诺。 The tableau Promise and the standard JavaScript Promise .画面Promise和标准 JavaScript Promise These two have different methods which currently results in issues.这两者有不同的方法,目前会导致问题。 You can pass a tableau Promise to Promise.resolve() to convert it into a standard JavaScript Promise.您可以将 tableau Promise 传递给Promise.resolve()以将其转换为标准的 JavaScript Promise。

return Promise.resolve(worksheet.applyFilterAsync(step.name, values, 'replace'));

When working with the promise you now have to work with then() , catch() and finally() instead of then() , otherwise() and always() .在使用 promise 时,您现在必须使用then()catch()finally()而不是then()otherwise()always()

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

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