Skip to content
On this page

useCancellablePromise

Prevents result to be set if the cancel is called

This uses usePromise

WARNING

Javascript doesn't provide a way to cancel promises, so this will execute the promise until the end.

Parameters

typescript
import { useCancellablePromise } from '@elonehoo/pistachio'

const cancellablePromise = useCancellablePromise(factory, lazy?)

const options = {
   /**
   * if `true` allows to catch exception when `exec()`
   * @default false
   */
  throwException?: boolean

  /**
   * Only executes on `exec`
   * @default false
   */
  lazy?: boolean
  /**
   * @description cancel the promise on component unmount
   * @default true
   */
  unmountCancel?: boolean
}

const cancellablePromise = useCancellablePromise(factory, options?)
import { useCancellablePromise } from '@elonehoo/pistachio'

const cancellablePromise = useCancellablePromise(factory, lazy?)

const options = {
   /**
   * if `true` allows to catch exception when `exec()`
   * @default false
   */
  throwException?: boolean

  /**
   * Only executes on `exec`
   * @default false
   */
  lazy?: boolean
  /**
   * @description cancel the promise on component unmount
   * @default true
   */
  unmountCancel?: boolean
}

const cancellablePromise = useCancellablePromise(factory, options?)
ParametersTypeRequiredDefaultDescription
factoryFunctiontrueFactory will be called every time the exec is called, the arguments will be passed to the factory. Required
lazybooleanfalsefalse

State

The useCancellablePromise function exposes the following reactive state:

typescript
import { useCancellablePromise } from '@elonehoo/pistachio'

const { promise, result, loading, error, cancelled } = useCancellablePromise()
import { useCancellablePromise } from '@elonehoo/pistachio'

const { promise, result, loading, error, cancelled } = useCancellablePromise()
StateTypeDescription
promisePromiseCurrent promise
resultanyResolved value
loadingbooleanWaiting for the promise to be resolved
erroranyPromise error
cancelledbooleanWas cancelled

Methods

The useCancellablePromise function exposes the following methods:

typescript
import { useCancellablePromise } from '@elonehoo/pistachio'

const { exec, cancel } = useCancellablePromise()
import { useCancellablePromise } from '@elonehoo/pistachio'

const { exec, cancel } = useCancellablePromise()
SignatureDescription
exec(args?)Resolves new promise
cancel(error?)Cancels by rejecting promise

Example

loading...
vue
<script setup lang="ts">
import { ref } from 'vue'
import { useCancellablePromise } from '@elonehoo/pistachio'

const {
  exec,
  loading,
  cancel,
  error,
  cancelled,
  result
} = useCancellablePromise(delay =>
  fetch(`https://reqres.in/api/users?delay=${delay}`).then(x => x.json())
)

const delay = ref(1)
</script>

<template>
  <div>
    <div>
      <label for="delayPromise">Delay seconds</label>
      <input v-model="delay" name="delayPromise">
    </div>
    <div>
      <button :disabled="loading" @click="exec(delay)">
        Execute
      </button>
      <button :disabled="!loading" @click="cancel()">
        Cancel
      </button>
    </div>

    <div v-if="loading">
      loading...
    </div>
    <div v-else-if="cancelled">
      <p>Request cancelled</p>
      <p>Result: {{ result }}</p>
      <p>Error: {{ error }}</p>
    </div>
    <div v-else>
      <p>Result: {{ result }}</p>
      <p>Error: {{ error }}</p>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useCancellablePromise } from '@elonehoo/pistachio'

const {
  exec,
  loading,
  cancel,
  error,
  cancelled,
  result
} = useCancellablePromise(delay =>
  fetch(`https://reqres.in/api/users?delay=${delay}`).then(x => x.json())
)

const delay = ref(1)
</script>

<template>
  <div>
    <div>
      <label for="delayPromise">Delay seconds</label>
      <input v-model="delay" name="delayPromise">
    </div>
    <div>
      <button :disabled="loading" @click="exec(delay)">
        Execute
      </button>
      <button :disabled="!loading" @click="cancel()">
        Cancel
      </button>
    </div>

    <div v-if="loading">
      loading...
    </div>
    <div v-else-if="cancelled">
      <p>Request cancelled</p>
      <p>Result: {{ result }}</p>
      <p>Error: {{ error }}</p>
    </div>
    <div v-else>
      <p>Result: {{ result }}</p>
      <p>Error: {{ error }}</p>
    </div>
  </div>
</template>

Released under the MIT License.