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

开发者可通过Aspect类封装提供切面能力的接口，用于对类方法进行前后插桩或替换实现，其中[addBefore()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-util#addbefore11)方法可在指定的类对象的原方法执行前插入一个函数，[replace()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-util#replace11)方法可将指定类的原方法替换为另一个函数。

参考如下示例：

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);
});