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
  • description
  • example
  • synchronous example
  • asynchronous example
  1. API reference

then

then :: (Function fn, Any|Promise<Any> input) => Any|Promise<Any> output

description

A utility function which will call the provided function fn with the input as soon as the input's value is available: if the input is not a Promise, then(fn, input) is equivalent to fn(input). Otherwise, it's equivalent value.then(fn). It can be useful in a function composition chain if you're unsure as to whether the input value will be a Promise or not. It's mostly used internally in conductor.

If your value is a Promise, then's result will also be a Promise, so you'll need to use the await keyword or Promise.prototype.then. Since then's purpose is to execute code when you're unsure as if the input is actually a Promise, this can seem paradoxal. However, the await keyword will work seamlessly on Promise or non-Promise values.

example

synchronous example

import { then } from 'conductor'

const double = x => 2 * x
then(double, 2) // 4

Here, our input value (2) is not a Promise, so this code is equivalent to double(2).

asynchronous example

import { then } from 'conductor'

const double = x => 2 * x
then(double, Promise.resolve(2)) // Promise<pending>
await(double, Promise.resolve(2)) // 4

Here, our input value (Promise.resolve(2)) is a Promise, so then returns a Promise. If you try calling it without using the await keyword or Promise.prototype.then, the result will be a Promise.

PrevioustakeNexttoLowerCase

Last updated 7 years ago