0

I am using pusher to listen to a particular event being sent from another server.

I have successfully bind to the event but my problem is the code moves past the binding and executes the rest of the code.

I have tried using Await but that doesn't help also

I can't use setTimeout because the message to be received in the pusher event is determined by if the user completes payment.

The code

await pusher.bind(trans_id, function(data) {
        if (data.message === 'success') {
            agent.add('Transaction was successful');
        } else {
            agent.add('Transaction was unsuccessful');
        }
    });

    agent.add('Transaction Successful');

    console.log('We got here');

As you can see, the code is not waiting for the pusher.bind to execute, it jumps straight to the next agent.add and console.log

Environment: Google Cloud Functions

The code is for DialogFlow

Any help please

1baga
  • 340
  • 4
  • 16

1 Answers1

0

Just continue your program logic inside the callback. pusher.bind finishes execution very quickly, it's the callback that gets executed later.

pusher.bind(trans_id, function(data) {
        if (data.message === 'success') {
            agent.add('Transaction was successful');
        } else {
            agent.add('Transaction was unsuccessful');
        }

        console.log('We got here');
    });
mihai
  • 37,072
  • 9
  • 60
  • 86