@bsgbryan/madul

A simple tool that makes writing async code fun & easy

Usage no npm install needed!

<script type="module">
  import bsgbryanMadul from 'https://cdn.skypack.dev/@bsgbryan/madul';
</script>

README

NPM GitHub top language nycrc config on GitHub Travis (.com) branch Snyk Vulnerabilities for GitHub Repo GitHub last commit (branch)

Mädūl

Madul is a simple set of tools that help you craft clean async code that scales ridiculously well, is fun to write & maintain, and is super simple to instrument

Super-quick overview

definition (getMessages.js)
const madul = {
  deps: ['/db'],
  $init: async ({ db, username }) =>
    await db.connectAs({ username })
  ,
  getMessagesFrom: async ({ friend, sentBefore, db }) => {
    const allMessages  = await db.getAllMessagesBefore({ timestamp: sentBefore })
    const fromMyFriend = await db.getMessagesFrom({ friend })

    const messagesFromMyFriend = sdk.
      iterable(allMessages).
      filter(m => fromMyFriend.includes(m))

    return messagesFromMyFriend
  }
}
In the above code:
  1. A madul is just an object literal
  2. The db (a file local to the project, denoted by the /) dependency is loaded asynchronously
  3. Dependencies are passed as named parameters to methods
  4. The $init method is guaranteed to be executed after madul has fully loaded, but before it's available for use; so you know that the db dependency will be properly setup and connected to as username
  5. sdk is a collection of helpful functions. The iterable sdk wraps the async library. sdk is easily configurable/customizable per madul.
usage (getMessagesFromAda.js)
const bootstrap = require('@bsgbryan/madul/bootstrap')

const main = async () => {
  const messageGetter = await bootstrap('/getMessages', { username: 'KatherineJohnson' })
  
  const oneHour    = 1000 * 60 * 60
  const sentBefore = Date.now() - oneHour
  
  const messages = await messageGetter.getMessagesFrom({ friend: 'Ada', sentBefore })

  console.log('My messages from Ada!', messages)
}

main()
In the above code:
  1. We pass the username used for connecting to the db to bootstrap
  2. We don't call $init directly; that's handled for us as part of bootstrap
  3. We don't pass the db dependency to getMessagesFrom; that's handled for us
  4. main is necessary here because Node.js doesn't support top level await in non-ES modules