Skip to content

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

License

Notifications You must be signed in to change notification settings

lamualfa/only-make

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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`
    })
  }
}

About

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

Topics

Resources

License

Stars

Watchers

Forks