npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

error-captured-loader

v0.1.0

Published

为 await 表达式自动注入异常处理函数,并以 `[err, res]` 的格式返回,适用于 async/await 的同步写法

Readme

error-captured-loader

webpack loader: 为 await 表达式自动注入异常处理函数,并以 [err, res] 的格式返回,适用于 async/await 的同步写法

async/await 异常处理

举个例子,异步函数如下

function asyncFunc (flag) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (flag) {
        resolve('resolve!!')
      } else {
        reject(new Error('reject..'))
      }
    }, 100)
  })
}
// 没有异常处理,异步函数抛出错误时程序会直接报错
async function test () {
  await asyncFunc(true)
  await asyncFunc() // 报错
}

使用 try catch

async function test () {
  await asyncFunc(true)
  try {
    await asyncFunc()
  } catch (err) {
    console.log(err)
  }
}

使用同步写法

// 包裹一个 Promise 用于处理异常
function resolvePromise (promise) {
  return new Promise((resolve) => {
    return promise.then(res => {
      resolve([null, res])
    }).catch(error => {
      resolve([error, null])
    })
  })
}

async function test () {
  const [err, res] = await resolvePromise(asyncFunc())
  console.log(err, res) // Error: reject..  null

  const [err1, res1] = await resolvePromise(asyncFunc(true))
  console.log(err1, res1) // null resolve!!
}

使用 loader 自动注入

直接编写,此 loader 会自动包裹一层上面的异常处理函数

// 源码
async function test () {
  const [err, res] = await asyncFunc()
  console.log(err, res) // Error: reject..  null

  const [err1, res1] = await asyncFunc(true)
  console.log(err1, res1) // null resolve!!
}

// 编译后
async function test () {
  const [err, res] = await resolvePromise(asyncFunc())
  console.log(err, res) // Error: reject..  null

  const [err1, res1] = await resolvePromise(asyncFunc(true))
  console.log(err1, res1) // null resolve!!
}

TODO

以下表达式暂时不会被转换,暂时只识别await 表达式,是一个声明变量,且变量是一个数组(用于解构)

// 表达式
await asyncFunc()
// 没有解构
const res = await asyncFunc()
// 表达式赋值而不是声明变量
let res
([err, res] = await asyncFunc())
// 没有解构,且不是声明变量
(res = await asyncFunc())