HTTPリクエストを送信するPromiseベースのAPI
// GETリクエスト
const res = await fetch('/api/users');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const users = await res.json();
// POSTリクエスト
const res2 = await fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice' }),
});fetchはHTTPエラー(404, 500など)でもrejectされないためres.okを必ずチェックする。AbortControllerでリクエストをキャンセルできる。