Skip to content
background-image background-image

Using node-fetch with timeout

This example shows how to use node-fetch with timeout.

Statement

// submit form data to external service
const body = new FormData();
body.append('username', 'abc');
try {
  // request will fail after 1s timeout
  const signal = AbortSignal.timeout(1000);
  const resp = await fetch('https://httpbin.org/post', { method: 'POST', body, singal });
  const data = await resp.text();
  return [];
} catch {
  // TimeoutError
  return [];
}