conductor
Why I'm building conductorGitHub
v1.5.0
v1.5.0
  • Introduction
  • Overview
    • Introduction
    • Core concepts
  • API reference
    • always
    • append
    • apply
    • arity
    • branch
    • capitalize
    • compose
    • concat
    • curry
    • curryN
    • delay
    • dump
    • entries
    • equals
    • equalsBy
    • factory
    • filter
    • findIndex
    • flatten
    • flip
    • forEach
    • get
    • head
    • ifElse
    • identity
    • into
    • isPromise
    • iterate
    • join
    • keys
    • map
    • merge
    • mergeBy
    • next
    • not
    • pluck
    • prepend
    • random
    • reduce
    • replace
    • slice
    • some
    • split
    • take
    • then
    • toLowerCase
    • transduce
    • transformers
      • transformers/filter
      • transformers/map
    • type
    • upsert
    • values
  • Guides
    • example use cases
    • checkGuards
Powered by GitBook
On this page
  1. Guides

checkGuards

const checkGuardsOld = (guards, transition) => async prevObj => {
  const guardValues = await Promise.all(
    guards.map(async callback => callback(prevObj, transition))
  )

  if (!guardValues.every(a => a === true)) {
    throw new Error(
      `Guard on transition from ${transition.from} to ${
        transition.to
      } returned false`
    )
  }

  return prevObj
}
checkGuards.js
const checkGuards = (guards, transition) => prevObj =>
  compose(
    ifElse(
      a => a === true,
      () => {
        throw new Error(
          `Guard on transition from ${transition.from} to ${
            transition.to
          } returned false`
        )
      },
      always(prevObj)
    ),
    some(a => a === false),
    thrush(prevObj),
    apply(branch),
    map(bind(prevObj, transition))
  )(guards)
Previousexample use cases

Last updated 7 years ago