Socket
Socket
Sign inDemoInstall

callforth

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    callforth

A tiny utility library to conveniently replace callbacks with Promises.


Version published
Weekly downloads
11K
decreased by-2.04%
Maintainers
1
Install size
15.5 kB
Created
Weekly downloads
 

Readme

Source

Callforth :running:

npm version minzipped size

A tiny utility library to replace callbacks with Promises where possible. Don't callback, callforth!

It simply includes a hand-full of functions I see myself re-implementing in nearly every project. So I might as well put them in a package.

Do things like:

await timeout(3000)
await eventOn(videoElement, "loadeddata")
const message = await eventOn(webWorker, "message")

Install :package:

npm install callforth

Now you can:

import { eventOn, timeout, polling } from "callforth"

Alternatively, include this script and:

<script src="./path/to/callforth.umd.js"></script>
<script>
  const { eventOn, timeout, polling } = window.callforth
</script>

API :eyes:

eventOn

const payload = await eventOn(target, successEvent, errorEvent)
Parameters
  • target : EventTarget - any object you can call addEventListener on.
  • successEvent : string - name of the event you want to await.
  • errorEvent : string (optional) - if this event fires, the promise is rejected.
Return Value
  • Promise<any> - wraps callback result (callbacks first argument)

timeout

await timeout(delay)
Parameters
  • delay : int - milliseconds after which the Promise should resolves.
Return Value
  • Promise<void>

polling

await polling(predicate, { maxTries, interval })
Parameters
  • predicate : any -> boolean - delay in milliseconds after which the Promise should resolve.
  • options : object (optional)
    • maxTries : int (default = 10) - maximum number of times to call predicate before giving up.
    • interval : int (default = 10) - delay in milliseconds between calls of predicate.
Return Value
  • Promise<void>

More Examples

async function loadScript(url) {
  let script = document.createElement("script")

  script.src = url

  await eventOn(script, "loaded")
}
async function primesLessThen(number) {
  primeWorker.postMessage(number)

  const result = await eventOn(primeWorker, "message")

  return result
}

FAQs

Last updated on 09 Oct 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc