Skip to content

Latest commit

 

History

History
119 lines (86 loc) · 2.67 KB

readme.md

File metadata and controls

119 lines (86 loc) · 2.67 KB

only-make

✨ One-liner helper to initialize complex local dependent variable.

NPM version License

Inspired by Rust's Block Expressions.

Before After
before after

hr

Features

  • 🔥 Zero dependencies
  • ⚡ Only 1 line of source code
  • 🚀 Supports on all Browser & Node.js versions
  • ✅ Fully typed

Installation

npm install only-make

Recipes

Basic

import { make } from 'only-make'

const value = make(() => {
  // Make & return the value
})

Asynchronous

import { make } from 'only-make'

const value = await make(async () => {
  // Make & return the value
})

Golang Like Error Handling

Synchronously
import { make } from 'only-make'

const [value, error] = make(() => {
  // If success
  return [new_value, null]

  // If error
  return [null, new_error]
})

if (!error) {
  // Handle `error`
}

// Use `value` safely
Asynchronously
import { make } from 'only-make'

const [value, error] = await make(async () => {
  // If success
  return [new_value, null]

  // If error
  return [null, new_error]
})

if (!error) {
  // Handle `error`
}

// Use `value` safely

Access this

import { make } from 'only-make'

class MyClass {
  doSomething() {
    const value = make(() => {
      // Use `this`
    })
  }
}