Callforth 🏃
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
await
const message = await
Install 📦
npm install callforth
Now you can:
Alternatively, include this script and:
API 👀
eventOn
const payload = await
Parameters
- target :
EventTarget
- any object you can calladdEventListener
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
Parameters
- delay :
int
- milliseconds after which the Promise should resolves.
Return Value
Promise<void>
polling
await
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.
- maxTries :
Return Value
Promise<void>
More Examples
{ let script = document scriptsrc = url await }
{ primeWorker const result = await return result}