ElectronicBabylonianLiterature/ebl-frontend

View on GitHub
src/http/cancellableFetch.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import Promise from 'bluebird'

export default function cancellableFetch(
  url: string,
  options: RequestInit = {}
): Promise<Response> {
  return new Promise((resolve, reject, onCancel) => {
    const abortController = new AbortController()
    fetch(url, {
      ...options,
      signal: abortController.signal,
    })
      .then(resolve)
      .catch(reject)
    onCancel && onCancel(() => abortController.abort())
  })
}