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

4
  • Please don't show screenshots of code. It's better to copy the code into the question so it's easier to search and read. Oct 20, 2018 at 17:13
  • I have removed the image @DougStevenson
    – 1baga
    Oct 20, 2018 at 18:01
  • which version of node.js you are using in firebase functions ? If version is Node 6 you can change the node version to 8 which is in beta right now or you can use babel to support async await in node 6.0 Oct 22, 2018 at 6:14
  • @AniketPawarI use 8... async await works already...
    – 1baga
    Oct 28, 2018 at 10:27

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.