JSPM

  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q57909F
  • License MIT

Lua pcall inspired - calls the function f with the given arguments in protected mode

Package Exports

  • pcall.js
  • pcall.js/dist/index.js
  • pcall.js/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pcall.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

PCALL.js

🚧 EARLY DEVELOPMENT -- DO NOT USE THIS 🚧

🚧 Lua pcall inspired - calls the 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 status code.

Installation

# install
npm install pcall.js

Usage

// some promise
function clearBit(num, i) {
  return new Promise((resolve, reject) =>
    setTimeout(() => {
      Math.random() > 0.5
        ? resolve(num & ~(1 << i))
        : reject('noop');
    }, 3_000),
  );
}

ESM

import pcall from 'pcall.js'

const [ok, xo] = await pcall(clearBit, 99, 6);

  console.log({ ok, xo })
  // { ok: true,  xo: 35 }
  // { ok: false, xo: false }

CJS

const pcall = require('pcall.js')

async function main() {
  const [ok, xo] = await pcall(clearBit, 99, 6);

  console.log({ ok, xo })
  // { ok: true,  xo: 35 }
  // { ok: false, xo: false }

  return ok && xo
}

main()
  .then(console.log)
  .catch(console.error)

Development

# dev
npm run dev

# build
npm run build

License

MIT