mgechev / aspect.js

JavaScript library for aspect-oriented programming using modern syntax.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interceptinn promises

dpinart opened this issue · comments

Hi

I've seen afterReject and afterResolve decorators in introduction post of the library here that I guess they are useful to intercept async methods but they are not present in the library. How can I intercept promise rejections?

Thanks

Hey! You can achieve this result with the following construct:

class Aspect {
  @afterMethod({
    methodNamePatter: /Target/,
    methodNamePattern: /async/
  })
  afterAsync(meta) {
    meta.method.result.then(() => {
      console.log('Equivalent to afterResove');
    });
  }
}

@Wove()
class Target {
  async() {
    return Promise.resolve(42);
  }
}