3.3.2 • Published 3 days ago

promise-loading-spinner v3.3.2

Weekly downloads
30
License
MIT
Repository
github
Last release
3 days ago

NPM version Downloads star this repo fork this repo CI Coverage Status

promise-loading-spinner

Advanced handling of loaders/spinners based on one or multiple Promises.

Why?

  • Show a spinner using a promise Spinner will disappear when promise is resolved
  • No Spinner for short operations Spinner won't show up for short operations so operation doesn't feel lazy
  • Support of multiple promises Spinner will disappear when the last unresolved promise is resolved
  • Operations in a sequence If there are multiple operations in a sequence the spinner will disappear when the last operation is finished

See this Codepen.

Install

$ yarn add promise-loading-spinner

Usage

This shows loading spinners based on promises.

import PromiseLoadingSpinner from 'promise-loading-spinner'

const loader = new PromiseLoadingSpinner({
  // options
})

// ...

const myPromise = new Promise((resolve) => {
  setTimeout(() => {
    resolve('done')
  }, 5000)
})

loader.loader(myPromise)

You can also use a wrapper function for a function that returns a promise or uses await:

const myFunction = loader.wrapFunction(async (url) => {
  const response = await fetch(url)
  const result = await response.json()
  return result
})

myFunction(someUrl)

It also exports a method decorator:

const loaderDecorator = loader.decorator.bind(loader)

class MyClass {
  @loaderDecorator()
  async foo() {
    // ...
  }
}

All ways to add a promise can also take a configuration object as an optional parameter.

OptionDescriptionType
skipDelaysSkip all delays (initDelay, delay)boolean

Examples:

// loader
loader(promise, { skipDelays: true })

// wrapFunction:
wrapFunction(fnc, { skipDelays: true })

// decorator:
class MyClass {
  @loaderDecorator({ skipDelays: true })
  async foo() {
    // ...
  }
}

Options

OptionDescriptionTypeDefault
delayTime (ms) until spinner will show up to handle short operations without a spinnerNumber300
closeDelayTime (ms) to wait until last promise is resolved to enable multiple operations in a sequence without a "flickering" spinnerNumber10
initDelayDelay (ms) after loader initialization to suppress spinners on page loadNumber1000
loaderElementthe element which represents the spinnerstring (selector) or HTMLElementjs-page-loader
classActiveclass name used to show the spinnerstringis-active

License

MIT © 2023 Jens Simon

3.3.2

3 days ago

3.3.1

7 days ago

3.3.0

9 days ago

3.2.0

7 months ago

3.0.3

1 year ago

3.1.0

1 year ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago