https://github.com/lamualfa/only-make
✨ One-liner helper to initialize complex local dependent variable.
https://github.com/lamualfa/only-make
make only
Last synced: 10 months ago
JSON representation
✨ One-liner helper to initialize complex local dependent variable.
- Host: GitHub
- URL: https://github.com/lamualfa/only-make
- Owner: lamualfa
- License: wtfpl
- Created: 2024-12-15T09:08:15.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-15T15:18:53.000Z (over 1 year ago)
- Last Synced: 2024-12-15T16:27:07.943Z (over 1 year ago)
- Topics: make, only
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
only-make
✨ One-liner helper to initialize complex local dependent variable.
Inspired by Rust's Block Expressions.
| Before | After |
| ------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------- |
|  |  |

## Features
- 🔥 Zero dependencies
- ⚡ Only 1 line of source code
- 🚀 Supports on all Browser & Node.js versions
- ✅ Fully typed
## Installation
```bash
npm install only-make
```
## Recipes
- [Features](#features)
- [Installation](#installation)
- [Recipes](#recipes)
- [Basic](#basic)
- [Asynchronous](#asynchronous)
- [Golang Like Error Handling](#golang-like-error-handling)
- [Synchronously](#synchronously)
- [Asynchronously](#asynchronously)
- [Access `this`](#access-this)
### Basic
```js
import { make } from 'only-make'
const value = make(() => {
// Make & return the value
})
```
### Asynchronous
```js
import { make } from 'only-make'
const value = await make(async () => {
// Make & return the value
})
```
### Golang Like Error Handling
###### Synchronously
```js
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
```js
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`
```js
import { make } from 'only-make'
class MyClass {
doSomething() {
const value = make(() => {
// Use `this`
})
}
}
```