0

running code that has a body similar to the one below, the code executes fine, but the connection doesn't close once it's finished, I've looked at node process doesn't exit after firebase once but I'm looking for something that won't give me an error every time that the function is used in the serverless framework.

const firebase = require('firebase-admin');

return new Promise((resolve, reject) => {
    var message = {
        notification: {
          title: `**`,
          body: `**`
        },
        condition: `**`
      };
      firebase.messaging().send(message).then((response) => {
        resolve(response);
      })
      .catch((error) => {
          reject(error);
      });
    });

How can I get it to exit this process without creating a serverless error?

Matt Deleeuw
  • 59
  • 1
  • 7

2 Answers2

0

Your question is a little vague, so I might be misunderstanding, but I think you simply need process.exit() to terminate the Node process.

const firebase = require('firebase-admin');

return new Promise((resolve, reject) => {
    var message = {
        notification: {
          title: `**`,
          body: `**`
        },
        condition: `**`
      };
      firebase.messaging().send(message).then((response) => {
        resolve(response);
        process.exit(0) // Addition; zero exit code indicates success
      })
      .catch((error) => {
          reject(error);
          process.exit(1) // Addition; non-zero exit code indicates something went wrong
      });
    });
john-goldsmith
  • 2,969
  • 1
  • 19
  • 18
0

I did some more research, and found the way to do this without exiting the AWS serverless function is to use the firebase.messaging().goOffline() function instead of the process.exit() function

const firebase = require('firebase-admin');

return new Promise((resolve, reject) => {
    var message = {
        notification: {
          title: `**`,
          body: `**`
        },
        condition: `**`
      };
      firebase.messaging().send(message).then((response) => {
        resolve(response);
        firebase.messaging().goOffline();
      })
      .catch((error) => {
          reject(error);
          firebase.messaging().goOffline();
      });
    });
Matt Deleeuw
  • 59
  • 1
  • 7