.subscribe - waiting for a subscribe function help

In my function, I am generating a QR code. The user then scans the QR code with a mobile wallet app. There is a subscribe function that waits for that connection and then calls an API.

The subscribe is not waiting. This works in regular Node.js locally - any thoughts on how to make it work?

/* Generate a QR Code */
    const qrCode = await client.generateQRCode();

    $respond({
      status: 200,
      body: qrCode, // This can be any string, object, Buffer, or Readable stream
    });
client.connect().subscribe(user => {
        console.log('new connexion', user);
        axios.post('https://hedera.dpub.xyz/version-test/api/1.1/wf/pd-hedera',{accountid: 'PIPE DREAM inside'})
    });

I get the QR code just fine - show it on screen - scan with the app, but subscribe is probably not waiting…

Usually when my code runs locally, and fails in pipedream, it is due to a missing “await”. At first glance, it looks like you’re expecting client.connect().subscribe to run synchronously. But the call to axios.post should be async. Maybe try one of two things:

  1. await the post request
client.connect().subscribe(async (user) => {
    console.log('new connexion', user);
    await axios.post('https://hedera.dpub.xyz/version-test/api/1.1/wf/pd-hedera',{accountid: 'PIPE DREAM inside'})
});
  1. Convert the call to client.connect().subscribe into a promise and then await both the subscription and post request.
const user = await new Promise((resolve) => {
    client.connect().subscribe(user => resolve(user));
});
console.log('new connexion', user);
await axios.post('https://hedera.dpub.xyz/version-test/api/1.1/wf/pd-hedera',{accountid: 'PIPE DREAM inside'});