0

I'm trying to wait on two promises and get both functions to be called. Below is my AWS Lambda, but I believe this is purely a Node problem.

In my log, I see @@BP12.1, and BP1-1, amd BP1-2, but I don't see BP1-3, BP1-4, and BP1-5. I can't see evidence that it called the static MessageSession.getAccessToken token. I'd like the code below to call both sendMessage and updateStatus, both of which return promises, and wait for the completion of them. I'd like sendMessage to actually call the getAccessToken and create the message.

I have a Promise to return something which joins two promises. What did I do wrong with the Promise nesting?

exports.handler = async(event, context, callback) => {
...
console.info('@@BP12.1: ');
var statusTypeName = 'AVAILABLE';
                (async() => {
                    await Promise.all([sendMessage(event), updateStatus(event, statusTypeName).promise]);
                })();
...
}

async function sendMessage(payload) {
    const promise = new Promise(function(resolve, reject) {
        console.info('@@BP1-1');
        let argumentParserResult = { ...};
        console.info('@@BP1-2');
        let accessToken = MessagingSession.getAccessToken(argumentParserResult).then((accessTokenResponse) => {
            console.info('@@BP3');
            argumentParserResult.accessToken = accessTokenResponse.access_token;
            let messagingSession = new MessagingSession(MessagingSession.getInitializedSolaceModule(), argumentParserResult);
            console.info('@@BP4');
            resolve(messagingSession);
        }).catch(error => {
            console.info('@@BP5');
            console.info("Error fetching access token: " + error);
            reject(error);
        })

    });
    return promise;
}
Woodsman
  • 901
  • 21
  • 61
  • Sounds like the promise returned by `getAccessToken` may be hanging. Also, do check out https://stackoverflow.com/questions/23803743 – CertainPerformance Nov 09 '22 at 00:38
  • You don't appear to be calling `await` on `accessToken`, which may be what is causing this issue – Dakeyras Nov 09 '22 at 00:43
  • @Dakeyras, just add await before MessagingSession.getAccessToken and everything else looks good? – Woodsman Nov 09 '22 at 00:54
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Nov 09 '22 at 02:30
  • "*`(async() => { await Promise.all([sendMessage(event), updateStatus(event, statusTypeName).promise]); })();`*" is pointless - same as just `Promise.all([sendMessage(event), updateStatus(event, statusTypeName).promise]);`, without the wrapper - and won't keep the lambda execution from being killed. Please show us the full code of the `handler`. – Bergi Nov 09 '22 at 02:32
  • @Bergi I've given you all you need to know about why this either does or doesn't work. It's as concise as it gets. I don't need a link to a massive academic argument on Promises or patterns. I just need an answer. – Woodsman Nov 09 '22 at 02:44
  • It's not academic argument. I'm telling you that "*I believe this is purely a Node problem*" is probably wrong, this is most likely a problem in how you are using lambda, but to diagnose that you'll need to post the full code of the handler. – Bergi Nov 09 '22 at 02:47
  • @Bergi I post a full set of code and ask for help. Some SO guy says "Hey it needs to be concise." Then I post a shortened set of code that concisely outlines where the problem is, and not with the fluff where it is not at, and I get asked for the full code. I just want to know, does it want yet another await, or a try catch withiin a try, or what does Node actually want to ***run** what I asked it to do. – Woodsman Nov 09 '22 at 02:50
  • "*does it want yet another await, or a try catch within a try*" - no. "*what does Node actually want to run*" - it should want to run to completion, but most likely lambda shuts the nodejs process down right after you sent the event response. – Bergi Nov 09 '22 at 02:52

0 Answers0