0.4.8 β€’ Published 2 days ago

pcall.js v0.4.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 days ago

Pcall.js

  • 🎌 Delegate Promise Resolutions
  • 🧬 Lifecycle Callback Options
  • πŸ“ Concise and Convenient Signature
  • πŸ“¦ Zero-Dependency
  • πŸ›‘ Avoid try/catch ~~HELL~ πŸ‘Ή
  • 🌟 Better Visibility and Control
  • 🌐 Works in Node.js (ESM/CJS) and all modern browsers
  • 〽️ Minimal Obsessive Disorder


Inspiration

Lua approach to error handling is simple yet powerful. ^Lua:5.4 ^Lua:8.4, ^Lua:8.5

πŸ”Ή pcall.js is heavily inspired by Lua pcall with superpowers πŸ¦„!


SYNOPSIS

pcall({f}, {arg1}, {...})

pcall() Calls function {f} with the given arguments in protected mode. This means that any error inside {f} is not propagated;

Instead, pcall catches the error and returns a tuple.

Its first element is the status code (a boolean), which is true if the call succeeds without errors. And all results from the call, on second element; [true, {res}]

In case of any error, pcall returns false plus the error message; [false, {err}]


Usage

# install
npm install pcall.js
// ESM
import Pcall from 'pcall.js'

// CJS
const Pcall = require('pcall.js')

Convert

// πŸ”» BEFORE
try {
  const res = await readFile('./package.json', { encoding: 'utf8' })
} catch(error) {
  console.error(error, 'πŸ”₯')
}

// ─────────────────
// πŸ”ΉAFTER
import Pcall from 'pcall.js'
const [err, user] = await Pcall(readFile, './package.json', { encoding: 'utf8' })

// πŸ”ΈTHROW
err && throw new Error("XYZZY", { cause: err });

Options

import { readFile } from 'node:fs/promises'
import Pcall from 'pcall.js'

const path = './package.json'
const opts = { encoding: 'utf8' }

const pcall = new Pcall({
  onSuccess: (args, res) => { /*Β·πŸ”ΉΒ·*/ },
  onFailure: (args, err) => { /*Β·πŸ”ΉΒ·*/ },
  transformOnSuccess: (args, res) => { /*Β·πŸ”ΉΒ·*/ },
  transformOnFailure: (args, err) => { /*Β·πŸ”ΉΒ·*/ },
  cleanup: (opts) => { /*Β·πŸ”ΉΒ·*/ },
  happy: false, // true will only return result
  trace: true,
})

const [err, res] = await pcall(readFile, path, opts)

console.log('err', '::', err)
console.log('res', '::', res)

Example

import { readFile } from 'node:fs/promises'
import Pcall from 'pcall.js'

const path = './test/pcall.test.json'
const opts = { encoding: 'utf8' }

const pcall = new Pcall({
  onSuccess: (args, res) => log('@:SUCCESS', { args, res }),
  onFailure: (args, err) => log('@:FAILURE', { args, err }),
  transformOnSuccess: (args, res) => JSON.parse(res),
  transformOnFailure: (args, cause) => new Error('BAD', { cause }),
  cleanup: (opts) => log('@CLEANUP', { opts }),
  trace: true,
})

const [err, res] = await pcall(readFile, path, opts)

console.error(':Pcall:[ERR]:<<', err, '>>')
console.debug(':Pcall:[RES]:<<', res, '>>')

Example Custom

import { readFile } from 'node:fs/promises'

const path = './test/pcall.test.json'
const opts = { encoding: 'utf8' }

const pread = new Pcall({
  transformOnSuccess: (args, res) => JSON.parse(res),
  fn: readFile,
})

const [err, res] = await pread(path, opts)

console.error(':Pcall:[ERR]:<<', err, '>>')
console.debug(':Pcall:[RES]:<<', res, '>>')

Example Happy

import { readFile } from 'node:fs/promises'

const path = './test/pcall.test.json'
const opts = { encoding: 'utf8' }

const pcall = new Pcall({
  transformOnSuccess: (args, res) => JSON.parse(res),
  happy: true,
})

const res = await pcall(readFile, path, opts)

console.log(res.hogo) // fuga

πŸ’‘ Check test/pcall.canary.js


Development

# run test playground in watch mode
npm run dev

# build production
npm run build

# build stub
npm run build:stub

TODO

  • πŸŒ€ Lifecycle Hooks
  • . πŸ”Œ Serializer
  • . 🧬 Parser
  • . πŸ“œ JSDoc
  • . πŸ”§ ESLint
  • o πŸ“– Docs
  • o ⚠️ Tests
  • . πŸ’‘ Examples

License

MIT

0.4.8-0

2 days ago

0.4.8

2 days ago

0.4.5-3

3 days ago

0.4.5

3 days ago

0.4.7

3 days ago

0.4.6

3 days ago

0.4.5-1

4 days ago

0.4.5-0

4 days ago

0.4.3

11 days ago

0.4.2

11 days ago

0.4.1-2

16 days ago

0.4.1-0

16 days ago

0.3.2

17 days ago

0.3.3

17 days ago

0.2.1-1

18 days ago

0.2.1-0

18 days ago

0.3.1-2

18 days ago

0.3.1-4

18 days ago

0.3.1-3

18 days ago

0.3.0

18 days ago

0.2.0

18 days ago

0.1.1-4

20 days ago

0.1.1-3

20 days ago

0.1.1-5

20 days ago

0.1.1-0

20 days ago

0.1.1-2

20 days ago

0.1.1-1

20 days ago

0.1.0

20 days ago

0.0.2-20

22 days ago

0.0.2-21

22 days ago

0.0.2-10

22 days ago

0.0.2-11

22 days ago

0.0.2-12

22 days ago

0.0.2-13

22 days ago

0.0.2-9

22 days ago

0.0.2-14

22 days ago

0.0.2-15

22 days ago

0.0.2-16

22 days ago

0.0.2-17

22 days ago

0.0.2-18

22 days ago

0.0.2-19

22 days ago

0.0.2-5

23 days ago

0.0.2-7

23 days ago

0.0.2-2

24 days ago

0.0.2-1

24 days ago

0.0.2-4

24 days ago

0.0.2-3

24 days ago

0.0.2-0

29 days ago

0.0.1

30 days ago