1.0.0 • Published 6 years ago

@rafaelrinaldi/hold-up v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

hold-up Build Status

Wait until a given condition is true

Install

npm install @rafaelrinaldi/hold-up

Usage

import holdUp from '@rafaelrinaldi/hold-up'

async function injectVendorScript() {
  // Asynchronously load a cheesy vendor script that triggers a bunch of other
  // subsequent requests
  const script = document.createElement('script')
  script.setAttribute('src', 'https://cdn.vendor.com/bundle.min.js')
  document.body.appendChild(script)

  // In order to use their SDK you must wait for the instances to be available
  await holdUp(() => window.$vendor && window.$vendor.sdk)

  // Finally use the SDK
  window.$vendor.sdk.show()
}

injectVendorScript()

API

holdUp(condition, [timeoutInterval], [retryInterval])

Returns a Promise instance that resolves whenever condition returns true. Rejects if condition is false or timeoutInterval times out.

condition

Type: Function

Function that the library will attempt to run until it gets a true value back.

timeoutInterval

Type: Number
Default: 8000

The maximum amount of time to wait until execution times out.

retryInterval

Type: Number
Default: 50

The interval between each attempt of calling condition.

License

MIT © Rafael Rinaldi