0

I want to send push notification with http request and then call process.exit() function but it doesn't work, process stops but push notification doesn't send. It works if i remove the process.exit function. Here is the code:

await page
.waitForSelector(".guest", { timeout: 5000 })
.then(async () => {
  console.log("Not logged in, exiting");
  await pushNotification("App has stopped, probably session expired.");
  process.exit();
})

async function pushNotification(msg) {
  var options = {
    method: "POST",
    url: "url",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      to: "",
      title: "",
      body: ``,
    }),
  };
  request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.body);
  });
}
  • 1
    You are not returning anything from your `pushNotification` function. Thus, it resolves immediately and you call `process.exit()` before the async `request(...)` has a chance to finish. Wrap your `request` into a promise and await this promise inside the `pushNotification` function or return this promise. And I'm pretty sure, there is already a promise based version of `request` – derpirscher Nov 05 '21 at 21:04
  • @derpirscher thank you! – lmoonwolf Nov 05 '21 at 21:14

0 Answers0