2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AbortController.abort(reason)をcatchした時のブラウザごとの挙動

Posted at

起こったこと

Fetch リクエストを AbortController.abort(reason) で中断したとき、catch(err) で受け取る err の中身が EdgeChrome では reason であるのに対し、Safari では AbortError となった。

controller = new AbortController();
const signal = controller.signal;

try {
  const response = await fetch(request);
  controller.abort(reason);
} catch (err) {
  console.log(err); // ここがブラウザによって異なる
}

reason を正しく取得する方法

reason を表示したい場合は、controller.signal.reason を利用する。

AbortController.abort() または AbortSignal.abort() によって処理が中断された場合、
signal.reason から渡された reason を取得できる。

この方法であれば ブラウザによる差異は生じず、仕様上も推奨される reason の取り方 である。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?