项目作者: bangwu

项目描述 :
This is a loader let you use the arrow function and async and await together
高级语言: JavaScript
项目地址: git://github.com/bangwu/async-arrow-loader.git
创建时间: 2017-04-24T07:19:20Z
项目社区:https://github.com/bangwu/async-arrow-loader

开源协议:MIT License

下载


async-arrow-loader

  1. yarn add async-arrow-loader --dev

We recommend using yarn, but you can also still use npm:

  1. npm install --save-dev async-arrow-loader

Within your webpack configuration object, you’ll need to add the async-arrow-loader to the list of modules, like so:

  1. module: {
  2. rules: [
  3. {
  4. test: /\.js$/,
  5. loader: 'async-arrow-loader',
  6. exclude: /(node_modules|bower_components)/
  7. }
  8. ]
  9. }

In the ES6, if you want to use the Arrow function and async and await together, just like this:

—-Old way:—-

  1. handleSubmit = () => {
  2. this.props.onSubmit()
  3. .then(() => {
  4. //do something here ...., after the promise resolve
  5. })
  6. }

—-New way:—-

  1. handleSubmit = async () => {
  2. await this.props.onSubmit()
  3. //do something here ...., after the promise resolve
  4. }

If you want to get the Promise response data, you can write like this:

—-Old way:—-

  1. handleSubmit = () => {
  2. this.props.onSubmit()
  3. .then((data) => {
  4. dosomething(data)
  5. //do something here ...., after the promise resolve
  6. })
  7. }

—-New way:—-

  1. handleSubmit = async () => {
  2. let data = await this.props.onSubmit()
  3. dosomething(data)
  4. //do something here ...., after the promise resolve
  5. }

If you want to catch the reject status:

—-Old way:—-

  1. handleSubmit = () => {
  2. this.props.onSubmit()
  3. .then((data) => {
  4. dosomething(data)
  5. //do something here ...., after the promise resolve
  6. }).catch((error) => {
  7. // do somethind after the promise reject
  8. })
  9. }

—-New way:—-

  1. handleSubmit = async () => {
  2. try{
  3. let data = await this.props.onSubmit()
  4. dosomething(data)
  5. //do something here ...., after the promise resolve
  6. } catch (e) {
  7. // do somethind after the promise reject
  8. }
  9. }