# 如何对异步方法进行插桩/替换

开发者可通过Aspect类封装提供切面能力的接口，用于对类方法进行前后插桩或替换实现，其中addBefore()方法可在指定的类对象的原方法执行前插入一个函数，replace()方法可将指定类的原方法替换为另一个函数。

参考如下示例：

```
import { util } from '@kit.ArkTS';

class Test1 {
  static data: string = 'initData';

  static async printData(arg: string) { // asynchronous method
    console.log('execute original printData');
    console.log('Test.data is' + Test1.data);
    console.log('arg', arg);
    return 0;
  }
}

// Pile insertion
util.Aspect.addBefore(Test1, 'printData', true,
  (classObj: Object, arg: string): void => {
    console.log('execute before');
    Reflect.set(classObj, 'data', 'dataChangedByBefore');
    console.log('arg is ' + arg);
  }
);

Test1.printData('m1').then((res) => {
  console.log('res = ' + res.toString());
  console.log('Test.data = ' + Test1.data);
});

class Test2 {
  static data: string = 'initData';

  static async printData(arg: string) { // asynchronous method
    console.log('execute original printData');
    console.log('Test.data is' + Test2.data);
    console.log('arg', arg);
    return 0;
  }
}

// replace
util.Aspect.replace(Test2, 'printData', true,
  // Replace with another asynchronous function
  async (classObj: Object, arg: string): Promise<number> => {
    console.log('execute instead');
    Reflect.set(classObj, 'data', 'dataChangedByInstead');
    console.log('arg is ' + arg);
    return Promise.resolve(100);
  });

Test2.printData('m1').then((res) => {
  console.log('res = ' + res.toString());
  console.log('Test.data = ' + Test2.data);
});
```