@aimwhy/run
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

解决问题

  1. 异常捕获,支持同步和异步
try {
   // 一堆逻辑
   
   try {
    // 一堆逻辑
   } catch() {

   }
} catch() {

}

到处写, 且嵌套层级过深不易读

2.异步方法调用,常出现竞态返回的问题

3.小巧、总代码量不到 60 行, 无依赖

import { run } from "@aimwhy/run";

function delay(ms, value) {
   return new Promise((res) => window.setTimeout(res, ms, value))
}

async function test(ms, v) {
   let [err, val] = await run(delay, [ms, v], true);

   if(err && err.isRace) {
      return console.warn('已被抢占')
   }

   console.log(val)
}

test(3000, 3)

setTimeout(() => {
   test(1000, 1)
})
import { run } from "@aimwhy/run";

function test(a) {
  console.log(a)
  throw new Error('test');
}

let [err, data] = run(test, [22]);

if (err) {
  return console.log(err);
}

console.log(data);

Package Sidebar

Install

npm i @aimwhy/run

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

9.7 kB

Total Files

11

Last publish

Collaborators

  • aimwhy