1.0.2 • Published 4 years ago

async-try-catch-loader v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

这个 loader 会自动在 async-await 中包裹 try-catch 语句

USAGE

yarn add async-try-catch-loader --dev

webpack loader 配置

{
  "loader": "async-try-catch-loader",
  "options": {
    "argument": "e", // catch 子句 参数名
    "padding": "console.log(e)" // catch body
  }
}

示例

(async () => {
  await genPromise()
  console.log(23322)
  await genPromise()
  console.log(233)
})()

转换成

(async () => {
  try {
    await genPromise()
    console.log(23322)
    await genPromise()
    console.log(233)
  } catch (e) {
    console.log(e)
  }
})()
const a = {
  methods: async function() {
    await genPromise()
    console.log(233)
  }
}
a.methods()

转换成

const a = {
  methods: async function() {
    try {
      await genPromise()
      console.log(233)
    } catch (e) {
      console.log(e)
    }
  }
}
a.methods()
const func2 = async () => {
  try {
    await genPromise()
    console.log(233)
  } catch (e) {
    console.log(e)
  }
}
func2()

转换成

const a = {
  methods: async function() {
    try {
      await genPromise()
      console.log(233)
    } catch (e) {
      console.log(e)
    }
  }
}
a.methods()
async function func() {
  await genPromise()
  console.log(233)
}
func()

转换成

async function func() {
  try {
    await genPromise()
    console.log(233)
  } catch (e) {
    console.log(e)
  }
}
func()
1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago