0

My current code works. However, it doesn't work exactly how I want it to work. Right now, my app is calling sendVerificationEmail, which is my own cloud function. Inside my cloud function, I'm calling sendCustomVerificationEmail, which is an Amazon Simple Email Service (SES) function.

I don't want my cloud function to return anything to my app until the SES function has confirmed that an email has been sent. How do I do this?

exports.sendVerificationEmail = functions.https.onCall((data, context) => {
  var emailAddress = data.emailAddress;
  var params = {
    EmailAddress: emailAddress,
    TemplateName: "MyAppVerificationEmail"
  };

  ses.sendCustomVerificationEmail(params, function(err, data) {
    if(err) {
      console.log(err);
      return err
    }
    else {
      console.log(data);
      // THIS IS WHERE I WANT MY CLOUD FUNCTION'S RETURN STATEMENT!

    }
  });

  return "Verification email sent."
  // I WANT TO MOVE THIS RETURN STATEMENT SO THAT IT'S CALLED ONLY AFTER THE ABOVE SES FUNCTION FINISHES

});

--- Thanks in advance! ---

ZigZagZak
  • 59
  • 1
  • 7
  • You should promisify the callback into a promise that you can use to return from the function. https://javascript.info/promisify – Doug Stevenson Jan 04 '20 at 20:12
  • Can you show an example of what that would look like? I've struggled for a long time to understand promises well enough to write my own. – ZigZagZak Jan 04 '20 at 20:32
  • I was going to, but then @Quentin closed as a duplicate before I could finish. Unforunately, the duplicate is far too generalized figure out a good solution for callable functions. Maybe Quentin would like to reopen this so I can add a solid, specific answer. – Doug Stevenson Jan 04 '20 at 20:39
  • The duplicate question is for precisely the same problem as this question. The accepted answer has some excellent examples of how to convert a callback based system into using promises. There's really no need to write *yet another* example of how to do it that happens to have different function and variable names in it. – Quentin Jan 04 '20 at 20:46
  • @Quentin One has to dig very deep into that question to find the sole solution that will work here. You could spend days evaluating everything there in order to discover the correct solution for this particular flavor of Cloud Functions. – Doug Stevenson Jan 04 '20 at 21:02
  • 1
    No, you just need to look at the accepted answer. It's the one with the very high rating and the big green tick. – Quentin Jan 04 '20 at 21:04
  • @ZigZagZak If you want to get started with this right now, the link I provided on how to promisify a callback is really all you need. You just have to convert the pattern there to this problem. It's fairly straightforward after you understand now the Promise constructor works (and assuming you also generally understand how promises work). – Doug Stevenson Jan 04 '20 at 21:04
  • It has nice big headings, and explains the concepts behind it. – Quentin Jan 04 '20 at 21:08
  • If anything, this should have been the dup: https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises – Doug Stevenson Jan 04 '20 at 21:25
  • @DougStevenson — If something about the answers on the duplicate is confusing then ZigZagZak could ask a new, much more specific, question about the thing they find confusing. This question is, however, at the exact same point as the question part of the duplicate. – Quentin Jan 04 '20 at 21:25
  • @Quentin Fully disagree. This question here is far more specific, and it relates to requirements by the Cloud Functions API being used here. The OP does not just need to "return a value from a async function". Most of the advice there simply does not apply and will be confusing. The OP needs to return a promise that resolves when the callback is complete. Very specific. – Doug Stevenson Jan 04 '20 at 21:29
  • @ZigZagZak In the face of an unhelpful closure, I suggest you just delete this question and start over. Make an attempt to promisify the async call to AWS, and if that doesn't work out the way you want, explain where you got stuck in your code and include your specific observation. – Doug Stevenson Jan 04 '20 at 21:38
  • @Quentin : Can you please reopen the question? I'm asking nicely. I admit that the heading of my question could have been better worded. But if I new the perfect way to ask the question, then I probably wouldn't need to answer the question in the first place. When a person is confused, the person often doesn't know exactly where they're confused, and therefore often can't ask the question with great specificity. Expecting me to ask a "much more specific" question is very unfair. – ZigZagZak Jan 04 '20 at 21:39
  • Just ask a new question and delete this one. No point in arguing with people who aren't reading the facts. – Doug Stevenson Jan 05 '20 at 02:55
  • https://stackoverflow.com/questions/59597211/did-i-correctly-promisify-and-thus-correctly-fix-this-firebase-cloud-function – ZigZagZak Jan 05 '20 at 05:13
  • I asked another question and it too was marked as a duplicate and redirected to an encyclopedic overview of promises. This website is broken. It now serves only very experienced coders. – ZigZagZak Jan 05 '20 at 18:37

0 Answers0