Cloud Stack Ninja

I am creating a simple Javascript class that i essentially want to have 'event listeners' on certain areas of a start() process. I have a class in one file and create an instance of that class in another file. I then want to attach a listener on part of the class and IF the user provides a function to the class listener, then execute it.


export class MyClass {
  constructor() {
    this.beforeStart = null;
    this.started = null;
    this.beforeEnd = null;
    this.ended = null;

  async start() {
    if (typeof this.beforeStart == 'function') await this.beforeStart();

    // .....start logic here..
    console.log('start logic here..');

    if (typeof this.started == 'function') await this.started();

  async end() {
    if (typeof this.beforeEnd == 'function') await this.beforeEnd();

    // .....end logic here..
    console.log('end logic here..');

    if (typeof this.ended == 'function') await this.ended();


const nClass = new MyClass();
nClass.beforeStart = () => {
  console.log('await beforeStart work here');
nClass.started = () => {
  console.log('await started work here');
nClass.beforeEnded = () => {
  console.log('await beforeEnded work here');
nClass.ended = () => {
  console.log('await ended work here');

Im looking to be able to have the listeners be awaited so the user can perform various task within the 'listener' hooks.

Read more here:

Content Attribution

This content was originally published by user616 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: