12 Currying and asynchrony (evolution)

Lesson goal Learn to apply currying to asynchronous code: build specialized request functions and chain asynchronous steps into a pipeline that waits for promises by itself.

Currying can be used effectively in asynchronous programming.

  1. Creating specialized functions
  2. We will defeat "callback hell" without promises
//  Let's create a base function for requests
// baseFetch :: String -> String -> a -> Promise
const baseFetch = (baseUrl) =>
                    (params) =>
                    (config = {}) =>
                      fetch(`${baseUrl}/${params}`, config)


const BASE_URL = 'https://example.com'
// bound to the base url
const useFetch = baseFetch (BASE_URL)

// create two specialized functions
// get the list of all users
const getAllUser = useFetch ('all-users')
// get the list of all books
const getAllBooks = useFetch ('all-books')

// now we can call the functions and get the data
getAllUser ().then(console.log)
getAllBooks ().then(console.log)

// you can add authorization tokens
// in a separate closure if needed
// xFetch :: String -> b -> String -> a -> Promise
const xFetch = (baseUrl) =>
          (headers) =>
          (params) =>
          (config = {}) =>
            fetch(`${baseUrl}/${params}`, {
              ...config,
              // merge the shared headers (with the token) with the request headers
              // so that config.headers does not replace them entirely
              headers: { ...headers, ...config.headers },
            })

const token = 'xxx'
// bound to the base url and to the headers at once
const useXFetch = xFetch (BASE_URL) ({
  'Content-Type': 'application/json',
  'authorization': `Bearer ${token}`,
})

// create two specialized functions
// get the list of all users
const getAllUserX = useXFetch ('all-users')
// get the list of all books
const getAllBooksX = useXFetch ('all-books')

Now below we will show two examples.

The first piece of code will be imperative and callback-based, while the second will use currying and closures without promises.

//  code that "smells bad"

// create a request function based on XMLHttpRequest:
// it works with events, without promises
// (we could wrap it in promises, of course, but today's topic is currying)

const fetchXML = (url, callback) => {
  const req = new XMLHttpRequest()
  req.addEventListener('load', () => {
    const data = JSON.parse(req.responseText)
    callback(data)
  })
  req.open('GET', url)
  req.send()
}


// What we want
// 1. When the "details" button of a user is clicked,
// we read the user's slug from the data attribute
// 2. Using the slug, we request the list of reviews
// 3. We take the slug of the latest review (first in the list) and make a request
// 4. We get the review details and take its text
// API: /reviews/<user slug> returns an array of reviews (latest first),
// /review-info/<review slug> returns a review with a text field

// the text we will show
var textReview = ''

// use event delegation on the whole container with the buttons
document.getElementById('users-wrapper')
  .addEventListener('click', (e) => {
    // get the element
    const el = e.target

    // if the click is not on a user button, ignore it
    if(!el.classList.contains('user-btn')) {
      return
    }

    // get the user's slug from the button
    const slugUser = el.dataset.slug // 'xksdfjdkhfd' is the slug

    // make sure there is a slug
    if(!slugUser) {
      return
    }

    // get the list of reviews by the user's slug
    fetchXML('https://example.com/reviews/' + slugUser, (reviews) => {
      // get the slug of the latest review
      const slugFirstReview = reviews[0].slug

      // request the review details
      fetchXML('https://example.com/review-info/' + slugFirstReview, (review) => {
        textReview = review.text
      })
    })
  })

We will use currying without promises and defeat "callback hell".

Let's refactor our code.

//  declarative code
// ==== NOTE: many of these helper functions already
// ==== exist in ready-made libraries;
// ==== we often write them here for learning purposes

// create a request function based on XMLHttpRequest:
// it works with events, without promises
// (we could wrap it in promises, of course, but today's topic is currying)

// fetchXML :: String -> String -> String -> (a -> *) -> XMLHttpRequest
const fetchXML = (baseUrl) => (params) => (id) => (callback) => {
  const req = new XMLHttpRequest()
  req.addEventListener('load', () => callback(JSON.parse(req.responseText)))
  req.open('GET', `${baseUrl}/${params}/${id}`)
  req.send()
  return req
}

// pipe :: ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x)


// let's build specialized functions on top of the function above
const BASE_URL = 'https://example.com'
const REVIEWS = 'reviews'
const REVIEW_INFO = 'review-info'

// a function with the base path fixed
const fetchBaseUrl = fetchXML(BASE_URL)

// a function that gets the list of reviews
const getReviews = fetchBaseUrl(REVIEWS)

// a function that gets the details of a specific review
const getReviewInfo = fetchBaseUrl(REVIEW_INFO)

// now let's create a helper for the click event
// so that we can use it in function composition
// clickElement :: Element -> (Event -> *) -> Undefined
const clickElement = (element) =>
                      (callback) =>
                        element.addEventListener('click', callback)

// What we want
// 1. When the "details" button of a user is clicked,
// we read the user's slug from the data attribute
// 2. Using the slug, we request the list of reviews
// 3. We take the slug of the latest review (first in the list) and make a request
// 4. We get the review details and take its text
// API: /reviews/<user slug> returns an array of reviews (latest first),
// /review-info/<review slug> returns a review with a text field

// head :: [a] -> a
const head = (arr) => arr[0]

// prop :: String -> a -> b
const prop = (prop) => (obj) => obj[prop]

// the text we will show
var textReview = ''

// get the container with the buttons
const btnWrapper =  document.getElementById('users-wrapper')

// run the composition right away
clickElement (btnWrapper) (pipe(
  (e) => e?.target, // get the element
  (el) => el?.dataset?.slug, // get the data attribute
  (slugUser) => getReviews (slugUser) ( // request all reviews
        pipe(
          head, // take the first review
          prop('slug'), // and its slug
          (slug) => getReviewInfo (slug) ( // request the review details by the slug
            (review) => {
              textReview = prop ('text') (review) // write the result into a global variable
            })
        )
      )
))

I'll admit it: the smell of bad code hasn't gone away. On top of that, this version lost the checks from the first one: that the click was on a user button and that the button has a slug. So a click outside a button sends a request to /reviews/undefined, and errors aren't handled here at all. We'll fix both shortcomings in the next version.

What if we add the power of promises to the code above and transform it?

//  Now let's get down to business
// copy clickElement, btnWrapper, head, prop and textReview from the previous example;
// BASE_URL, REVIEWS, REVIEW_INFO, getReviews and getReviewInfo are declared here again,
// so the two blocks can't be run together as a single piece
// let's rewrite our data-fetching function
// so that it returns a promise itself

// fetchClassic :: String -> String -> String -> Promise
const fetchClassic = (baseUrl) =>
                        (params) =>
                        (id) =>
                        fetch (`${baseUrl}/${params}/${id}`)
                          .then ((res) => {
                            // fetch does not reject on responses like 404 or 500,
                            // so we check the status ourselves
                            if (!res.ok) throw new Error(`HTTP ${res.status}`)
                            return res.json()
                          })

const BASE_URL = 'https://example.com'
const REVIEWS = 'reviews'
const REVIEW_INFO = 'review-info'

// get the list of reviews and the details of a review
const getReviews = fetchClassic (BASE_URL) (REVIEWS)
const getReviewInfo = fetchClassic (BASE_URL) (REVIEW_INFO)


// We'll have a super pipe that waits for promises
// under the hood: once a step returns a promise,
// the next steps go through then, and the result is a promise too
// pipeAsync :: ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
const pipeAsync = (...fns) => (x) => fns.reduce((v, f) => {
  return v instanceof Promise
    ? v.then(f)
    : f(v)
}, x)

// get the text of the user's latest review by the user's slug
// loadReviewText :: String -> Promise String
const loadReviewText = pipeAsync(
  getReviews, // request all reviews
  head, // take the first review
  prop('slug'), // and its slug
  getReviewInfo, // request the review details by the slug
  prop('text'), // take the text field
  (text) => textReview = text, // store it in the global variable
)

// the same checks as in the first version: the click is on a user button
// and the button has a slug; other clicks are ignored
// onUserButton :: (String -> *) -> Event -> Undefined
const onUserButton = (handler) => (e) => {
  const el = e.target
  if (!el.classList.contains('user-btn') || !el.dataset.slug) {
    return
  }
  handler(el.dataset.slug)
}

// run the improved version:
// pipeAsync handles promises under the hood, and an error at any step
// (network, response status, empty list of reviews) ends up in catch
clickElement (btnWrapper) (onUserButton ((slug) =>
  loadReviewText (slug)
    .catch ((error) => console.error('Failed to load the review:', error.message))
))

Key points

  • The curried base request function baseFetch keeps the base URL and the path in closures, and specialized functions such as getAllUser and getAllBooks are built from it through calls. Headers with a token can be kept in a separate closure, as in xFetch, from which getAllUserX and getAllBooksX are built.
  • Currying and pipe without promises make callback-based code declarative, but the smell of bad code doesn't go away: errors aren't handled in that version at all.
  • pipeAsync waits for promises by itself: once a step returns a promise, the next steps go through then, and the result is a promise too.
  • In a promise-based pipeline, an error at any step — the network, the response status, an empty list — ends up in a single catch.

Exercises

Exercise 1

In the starter, fakeFetch simulates a request: after 5 ms it returns the response stored in DB under the URL, or rejects the promise if there is no such URL. Write a curried base function createRequest :: String -> String -> Number -> Promise Object that takes a base URL, a resource and an id. Fix the base URL https://example.com in it and derive the specialized functions getUser and getBook. Print the name of user 1 and the title of book 7.

Starter code

// fake server data: URL -> response
const DB = {
  'https://example.com/users/1': { name: 'John', favoriteBook: 7 },
  'https://example.com/users/2': { name: 'Anna', favoriteBook: 3 },
  'https://example.com/books/3': { title: 'Functional-Light JavaScript' },
  'https://example.com/books/7': { title: 'Structure and Interpretation of Computer Programs' },
}

// simulated request: after 5 ms it returns the response for the URL
// or rejects the promise if there is no such URL
// fakeFetch :: String -> Promise Object
const fakeFetch = (url) => new Promise((resolve, reject) => {
  setTimeout(() => {
    if (url in DB) resolve(DB[url])
    else reject(new Error(`404 ${url}`))
  }, 5)
})

// your code

Expected output

John
Structure and Interpretation of Computer Programs
Show solution
// fake server data: URL -> response
const DB = {
  'https://example.com/users/1': { name: 'John', favoriteBook: 7 },
  'https://example.com/users/2': { name: 'Anna', favoriteBook: 3 },
  'https://example.com/books/3': { title: 'Functional-Light JavaScript' },
  'https://example.com/books/7': { title: 'Structure and Interpretation of Computer Programs' },
}

// simulated request: after 5 ms it returns the response for the URL
// or rejects the promise if there is no such URL
// fakeFetch :: String -> Promise Object
const fakeFetch = (url) => new Promise((resolve, reject) => {
  setTimeout(() => {
    if (url in DB) resolve(DB[url])
    else reject(new Error(`404 ${url}`))
  }, 5)
})

// createRequest :: String -> String -> Number -> Promise Object
const createRequest = (baseUrl) => (resource) => (id) => fakeFetch(`${baseUrl}/${resource}/${id}`)

// the base URL is kept in the closure
const api = createRequest('https://example.com')

// specialized functions: the resource is fixed too, they wait only for an id
const getUser = api('users')
const getBook = api('books')

getUser(1).then((user) => console.log(user.name))
getBook(7).then((book) => console.log(book.title))

Exercise 2

The starter already has getUser and getBook from the first exercise. Write pipeAsync, a pipe that waits for promises by itself, and use it to build loadFavoriteBook :: Number -> Promise String: request the user by id, take the favoriteBook field, request that book and return its title. The output is already in the starter: showFavoriteBook prints the title of user 2's favorite book, then tries user 5, who is not in DB, and handles the error with catch.

Starter code

// fake server data: URL -> response
const DB = {
  'https://example.com/users/1': { name: 'John', favoriteBook: 7 },
  'https://example.com/users/2': { name: 'Anna', favoriteBook: 3 },
  'https://example.com/books/3': { title: 'Functional-Light JavaScript' },
  'https://example.com/books/7': { title: 'Structure and Interpretation of Computer Programs' },
}

// fakeFetch :: String -> Promise Object
const fakeFetch = (url) => new Promise((resolve, reject) => {
  setTimeout(() => {
    if (url in DB) resolve(DB[url])
    else reject(new Error(`404 ${url}`))
  }, 5)
})

// createRequest :: String -> String -> Number -> Promise Object
const createRequest = (baseUrl) => (resource) => (id) => fakeFetch(`${baseUrl}/${resource}/${id}`)
const api = createRequest('https://example.com')
const getUser = api('users')
const getBook = api('books')

// your code: pipeAsync and loadFavoriteBook

// an error at any step ends up in catch
// showFavoriteBook :: Number -> Promise Undefined
const showFavoriteBook = (userId) =>
  loadFavoriteBook(userId)
    .then((title) => console.log(title))
    .catch((error) => console.log(`Request failed: ${error.message}`))

// user 2 first, then user 5, who does not exist
showFavoriteBook(2).then(() => showFavoriteBook(5))

Expected output

Functional-Light JavaScript
Request failed: 404 https://example.com/users/5
Show solution
// fake server data: URL -> response
const DB = {
  'https://example.com/users/1': { name: 'John', favoriteBook: 7 },
  'https://example.com/users/2': { name: 'Anna', favoriteBook: 3 },
  'https://example.com/books/3': { title: 'Functional-Light JavaScript' },
  'https://example.com/books/7': { title: 'Structure and Interpretation of Computer Programs' },
}

// fakeFetch :: String -> Promise Object
const fakeFetch = (url) => new Promise((resolve, reject) => {
  setTimeout(() => {
    if (url in DB) resolve(DB[url])
    else reject(new Error(`404 ${url}`))
  }, 5)
})

// createRequest :: String -> String -> Number -> Promise Object
const createRequest = (baseUrl) => (resource) => (id) => fakeFetch(`${baseUrl}/${resource}/${id}`)
const api = createRequest('https://example.com')
const getUser = api('users')
const getBook = api('books')

// prop :: String -> a -> b
const prop = (key) => (obj) => obj[key]

// a pipe that waits for promises by itself: once a step returns a promise,
// the next steps go through then
// pipeAsync :: ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
const pipeAsync = (...fns) => (x) => fns.reduce((v, f) => (v instanceof Promise ? v.then(f) : f(v)), x)

// loadFavoriteBook :: Number -> Promise String
const loadFavoriteBook = pipeAsync(
  getUser, // request the user
  prop('favoriteBook'), // the id of their favorite book
  getBook, // request the book
  prop('title'), // its title
)

// an error at any step ends up in catch
// showFavoriteBook :: Number -> Promise Undefined
const showFavoriteBook = (userId) =>
  loadFavoriteBook(userId)
    .then((title) => console.log(title))
    .catch((error) => console.log(`Request failed: ${error.message}`))

// user 2 first, then user 5, who does not exist
showFavoriteBook(2).then(() => showFavoriteBook(5))

Playground

Write and run JavaScript right here — Ramda (R) and Sanctuary (S) are available.

Connected:
playground
Output
—