@jill64/async-observer
TypeScript icon, indicating that this package has built-in type declarations

1.2.4 • Public • Published

@jill64/async-observer

npm-version npm-license npm-download-month npm-min-size ci.yml

🔭 Make Promise state observable as a string

Example

import { observable } from '@jill64/async-observer'

const { status, observed } = observable({
  // FULFILLED => IDLE at 100ms
  resolveToIdle: 100,
  // REJECTED => IDLE at 500ms
  rejectToIdle: 500
})

const run = observed(async () => {
  // e.g. Duration as 1000ms
  await yourAsyncFunction()
})

let str = ''

status.subscribe((status) => {
  str = status
})

// str => IDLE

run()

// str => PENDING

// ↓ After the 1000ms

// str => FULFILLED

// ↓ After the 100ms

// str => IDLE

Compatibility for svelte store

Since status is implemented according to svelte store, it can be used in svelte applications as follows

<script>
  import { observable } from '@jill64/async-observer'

  const { status, observed } = observable()

  $: set = observed(async () => {
    await yourAsyncFunction()
  })
</script>

<button on:click={set}>Button</button>

<div>{$status}</div>

License

MIT

Package Sidebar

Install

npm i @jill64/async-observer

Weekly Downloads

8,962

Version

1.2.4

License

MIT

Unpacked Size

8.45 kB

Total Files

13

Last publish

Collaborators

  • jill64