Skip to content

freckle/cancelable-promise-js

Repository files navigation

@freckle/cancelabe-promise

Utilities to create a promise that can be canceled.

Install

yarn add @freckle/cancelable-promise

Versioning and release process

See RELEASE.md.

Usage

const expensiveOperation = () => fetchData().then(parse).then(transform)

const Component = () => {
  const [result, setResult] = setState(null)
  useEffect(() => {
    const promise = makeCancelable(expensiveOperation).then(res => {
      setResult(res)
    })
    return () => {
      // Cancel on cleanup
      promise.cancel()
    }
  })

  return result
}

LICENSE