Caught error still trigger `.catch` method for Promises

huangapple go评论110阅读模式
英文:

Caught error still trigger `.catch` method for Promises

问题

My problem

我有一个异步方法 populate(),它尝试执行某些操作,如果失败,则捕获错误并调用另一个异步方法 populateFromServer()(它也可能失败)。
我在 Promise.all() 中调用第一个方法,然后在没有任何尝试成功的情况下使用 catch。然而,由于某种原因,.all() 始终会因为 populate() 而触发,尽管应该被捕获。
以下是我的代码的简化版本(typescript):

abstract class Database {
  public static async populate() {
    try {
      await populateFromLocalFile();
    } catch (err) {
      await this.populateFromServer();
    }
  }

  private static async populateFromServer() {
    await fetchFromServer();  // 可能失败,例如如果没有网络
  }
}

还有其他地方的代码:

Promise.all([
  ChildDB1.populate(),
  ChildDB2.populate(),
  ChildDB3.populate(),
])
.then(() => /* 数据填充成功(本地或远程) */)
.catch(() => /* 没有任何尝试成功 */)

尽管我已经捕获了 populate() 的失败,并且 populateFromServer() 也正常工作(我通过日志进行了检查),但我的代码似乎在 Promise.all 后被捕获。为什么?如何修复这个问题?

What I tried

Returning Promise.resolve()

有人建议在 catch 块的末尾返回 Promise.resolve(),但这并没有解决问题:

  public static async populate() {
    try {
      await populateFromLocalFile();
    } catch (err) {
      await this.populateFromServer();
      return Promise.resolve()
    }
    return Promise.resolve()
  }

Using .then

我还尝试使用 .then 的方式,但问题仍然存在:

abstract class Database {
  public static async populate() {
    await populateFromLocalFile()
      .then(() => doStuff())
      .catch(async () => {
          console.log('error happened here');
          await this.populateFromServer();
      });
  }

  private static async B() {
    await fetchFromServer();
  }
}
英文:

My problem

I have an asynchronous method populate() that tries to do something, and if it fails, catches the error and calls another asynchronous method populateFromServer() (which can fail too).
I call the first in a Promise.all() with a catch afterwards in case no attempt worked. However, .all() always trigger because of populate(), for some reason, although it should be caught.
Here is a simplified version of my code (typescript):

abstract class Database {
  public static async populate() {
    try {
      await populateFromLocalFile();
    } catch (err) {
      await this.populateFromServer();
    }
  }

  private static async populateFromServer() {
    await fetchFromServer();  // Might fail eg. if there is no internet
  }
}

And somewhere else:

Promise.all([
  ChildDB1.populate(),
  ChildDB2.populate(),
  ChildDB3.populate(),
])
.then(() => /* population worked (either local or distant) */)
.catch(() => /* no population attempt worked */)

My code seems to get caught after the Promise.all when populate() fails, even though I caught it and populateFromServer() worked perfectly fine (I checked with logs). Why? How can I fix that?

What I tried

Returning Promise.resolve()

Someone suggested to return Promise.resolve() at the end of the catch block, but it doesn't fix the problem:

  public static async populate() {
    try {
      await populateFromLocalFile();
    } catch (err) {
      await this.populateFromServer();
      return Promise.resolve()
    }
    return Promise.resolve()
  }

Using .then

I also tried using the other way with .then but I still have that problem

abstract class Database {
  public static async populate() {
    await populateFromLocalFile()
      .then(() => doStuff())
      .catch(async () => {
          console.log('error happened here');
          await this.populateFromServer();
      });
  }

  private static async B() {
    await fetchFromServer();
  }
}

答案1

得分: 0

代码部分不要翻译
The problem was in the .then method of my Promise.all

Promise.all([
  ChildDB1.populate(),
  ChildDB2.populate(),
  ChildDB3.populate(),
])
.then(() => /* stuff */)  // <-- this is the part that failed silently
.catch(() => /* other stuff */)
英文:

The problem was in the .then method of my Promise.all

Promise.all([
  ChildDB1.populate(),
  ChildDB2.populate(),
  ChildDB3.populate(),
])
.then(() =&gt; /* stuff */)  // &lt;-- this is the part that failed silently
.catch(() =&gt; /* other stuff */)

huangapple
  • 本文由 发表于 2023年3月21日 01:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定