Skip to content
On this page

usePromise

Provides promise state

Parameters

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

usePromise(factory, lazy?)

usePromise(factory, { lazy?, throwException? })
import { usePromise } from '@elonehoo/pistachio'

usePromise(factory, lazy?)

usePromise(factory, { lazy?, throwException? })
ParametersTypeRequiredDefaultDescription
factoryFunctiontrueFactory will be called every time the exec is called, the arguments will be passed to the factory. Required
lazybooleanfalsefalseDefers execution of the factory until exec is called
throwExceptionbooleanfalsefalseMakes exec throw exceptions, when false the error will be handled only by the usePromise

WARNING

If factory argument has parameters, if lazy is false or not specified, the factory will be called without parameters. A warning will be raised in dev, to remove the warning please pass lazy:false

State

The usePromise function exposes the following reactive state:

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

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

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

Methods

The usePromise function exposes the following methods:

SignatureDescription
exec(args?)Resolves new promise

TIP

You can pass throwException on the last argument of the exec to override the default behaviour

Example

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

const timeout = ref(1000)
const throwError = ref(false)
const { exec, error, loading, result } = usePromise((ms) => {
  if (throwError.value)
    return Promise.reject(new Error('Throw Error checked'))

  return new Promise(res => setTimeout(() => res('sucess'), ms))
})
</script>

<template>
  <div>
    <label for="timeout">
      Duration (ms)
      <input v-model.number="timeout" type="number" name="timeout">
    </label>
    <label for="error">
      Reject promise
      <input v-model="throwError" type="checkbox" name="error">
    </label>

    <button @click="exec(timeout)">
      Execute
    </button>

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

const timeout = ref(1000)
const throwError = ref(false)
const { exec, error, loading, result } = usePromise((ms) => {
  if (throwError.value)
    return Promise.reject(new Error('Throw Error checked'))

  return new Promise(res => setTimeout(() => res('sucess'), ms))
})
</script>

<template>
  <div>
    <label for="timeout">
      Duration (ms)
      <input v-model.number="timeout" type="number" name="timeout">
    </label>
    <label for="error">
      Reject promise
      <input v-model="throwError" type="checkbox" name="error">
    </label>

    <button @click="exec(timeout)">
      Execute
    </button>

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

Released under the MIT License.