0.3.0 • Published 6 years ago

wait-and-go v0.3.0

Weekly downloads
26
License
-
Repository
github
Last release
6 years ago

Wait and go

Tiny util to create cached + "wait-on" callbacks

Installation

yarn add wait-and-go

Usage

import createWaiter from "wait-and-go"

// Create a Hachi
const afterAuth = createWaiter("After you are authorized")

// Call it somewhere
afterAuth(token => {
  console.log("Authorized", token)
})

// Resolve anywhere
afterAuth.do(() => Promise.resolve("my-secret-token"))

How it works?

It's just a more accurate version of:

let resolver
let promise = new Promise(r => {
  resolver = r
})

const auth = () => {
  promise = Promise.resolve("my-secret-token")
  resolver(promise)
}

const afterAuth = cb => promise.then(cb)

afterAuth(token => {
  console.log("Authorized", token)
})

React hook (WHY NOT LUL)

import createWaiter from "wait-and-go"
import { useWaiter } from "wait-and-go/react"

const afterAuth = createWaiter("After you are authorized")

const UserInfo = () => {
  const { isLoaded, data } = useWaiter(afterAuth)

  return isLoaded && <span>{data.nickname}</span>
}

// Somewhere
afterAuth({ nickname: "Kelin2025" })

With some additions

0.3.0

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago