Provide a promise that will be fulfilled upon the occurrence of an event

Currently utilizing the WhatsApp Web JS package and have created this function. I am trying to make it return a client that is ready to use eventually. I attempted using a promise that resolves when the "ready" event is triggered, but it seems like the event never triggers. Any suggestions?

  await mongoose.connect(process.env.MONGODB_URI!)

  const store = new MongoStore({ mongoose: mongoose })
  const client = new Client({
    authStrategy: new RemoteAuth({
      store: store,
      backupSyncIntervalMs: 300000,
    }),
  })

  console.log("initializing client")
  client.initialize()

  return new Promise<wweb.Client>((resolve, reject) => {

    client.on("ready", () => {
      console.log("client is ready")
      resolve(client)
    })
  })
}

Answer №1

To handle errors, consider rejecting the promise:

  await mongoose.connect(process.env.MONGODB_URI!)

  const store = new MongoStore({ mongoose: mongoose })
  const client = new Client({
    authStrategy: new RemoteAuth({
      store: store,
      backupSyncIntervalMs: 300000,
    }),
  })

  console.log("initializing client")
  client.initialize()

  return new Promise<wweb.Client>((resolve, reject) => {

    client.on("ready", () => {
      console.log("client is ready")
      resolve(client)
    })

    client.on("auth_failure", () => {
      console.log("Authentication failure")
      reject()
    })
  })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Use a spy to mock a component method using karma and jasmine

Encountering this error message during testing: ERROR: 'Error during cleanup of component' The issue stems from the following code snippet : ngOnDestroy(){ methodCallToMock() } To address this, I need to mock the methodCallToMock() functi ...

Tips for resolving the issue of receiving a warning about passing "onClick" to a "<Link>" component with an `href` of `#` while having "legacyBehavior" enabled in Next.js

My current project is displaying a lot of warnings in the browser console and I'm unsure about the reasons behind it. The warnings include: "onClick" was passed to with href of /discovery/edit but "legacyBehavior" was set. The l ...

Dynamic Webpage Content

I am currently developing a system that involves a network of sensors communicating with a Linux machine. My goal is to create a web interface that can display real-time sensor status updates and automatically refresh them whenever there is a change in sen ...

Resetting the mocked dependency in Jest and Supertest: a handy guide

I seem to be encountering an issue with resetting jest mocks of a dependency after it has been used once by supertest. Any help or tips would be greatly appreciated. Below is my API test utilizing supertest: import request from 'supertest'; imp ...

Generate a JSON object for every row of data in an Excel spreadsheet

Currently, I am delving into webpack for SheetJS. My experience with webpack is still growing, and I'm a complete novice when it comes to SheetJS. Rather than just getting back one JSON object containing all the Excel data, my goal is to retrieve one ...

What could be the reason for this Javascript code not functioning as intended, failing to generate a random number between 1 and 3 when I click on any of the buttons

Could someone help me with generating a random number between 1 and 3 each time I click on one of the buttons (rock, paper, scissors)? I am new to programming and not sure what I'm doing wrong. <!doctype html> <html lang="en"> <head& ...

What is preventing my jQuery carousel from displaying?

I added a carousel to my website, but I'm having trouble getting it to display. Can someone help me troubleshoot? My HTML: <!--Sponsor Carousel--> <link href="css/style.css" rel="stylesheet" media="screen"> ...

Creating dynamic content in the Ajax-enabled Smart Admin theme: A step-by-step guide

As I work on developing an application for a client, my focus is on creating a web service using Laravel5 for the backend. To enhance the user interface of this web service, I have chosen to incorporate the Smart Admin Theme, specifically utilizing the Aja ...

Directives may not function as expected in the invoked view, however, they work properly on the index.html page

Hello there, I've recently started using AngularJS for building applications and encountered a strange issue. I tried looking for similar problems but couldn't find a solution, which is why I'm reaching out to you all! My project specific ...

Adjust the color of a glyphicon when the text changes using Jquery

Here is a snippet of code that I am working with : <div class="col-xs-6"> @Html.TextBoxFor(model => model.TagName, new {@class = "textBoxTextFormat", @id = "newTagText"}) </div> <div class="col-xs-3" > <a href="#" id="addNe ...

The responsive dropdown menu is failing to show the elements within the array

My website has a responsive navbar that updates values based on selected tabs. https://i.sstatic.net/Fl6tM.png Now, I'm trying to replicate the same functionality in mobile view by using a select menu instead of a navbar. https://i.sstatic.net/JGrH ...

The React input field seems to be unresponsive to updates

I'm working on a React/Next application where users can create and update notes/jobs. However, I am facing an issue with my onChange={handleChange} function when trying to edit the input fields. Can anyone point out what might be going wrong? Thank y ...

Concealing a destination URL file with Rewrite in a configuration supporting multiple languages

I'm currently working on a rewrite feature where I need to mask the destination from '/dest' to '/new' The guidance from Next.js documentation includes the following code snippet module.exports = { async rewrites() { return [ ...

"Encountered a problem while navigating to a different page in Next.js

I encountered an error while transitioning to another page in Next.js 1 of 3 unhandled errors Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'pagenum') Source pages\dpt[dptid]\rewayat[rewid][subrewid]& ...

Running a JavaScript script within MongoDB

Seeking guidance on running a JavaScript file within MongoDB. Here is a snippet of code from my JS file: function loadNames() { print("name"); } My attempt to execute the file from the command prompt: mongo test.js resulted in the following error: ...

Setting default values for Select2 input with tagging in ASP.NET MVC is a simple process that can greatly enhance the user experience

I am currently utilizing a Select2 input box as a tag controller in my view: CSHTML <input id="tagSelector" type="hidden" style="width: 300px"/> JS $('#tagSelector').select2({ placeholder: 'Select a tag...', ...

PhantomJs is only providing partial responses

I have been attempting to retrieve a response from the following URL using PhantomJS:- https://www.trivago.com/api/v1/bin/accommodation/2891353/deals?iPathId=34812&iRoomType=1&aRooms=&aDateRange%5Barr%5D=2017-05-24&aDateRange%5Bdep%5D=2017 ...

Error message: Client side exception encountered in application - Next.js React app running exclusively in production mode

Issues have arisen specifically with IE and DJ Browser when using a Next.js React UI application in production. Daily, there are around 1000 client-side exceptions occurring randomly. The error message displayed on the console is quite peculiar: Object ...

The variable ReactDOMClient is not recognized

I recently started learning ReactJS by taking a beginners course on Pluralsight. However, I am struggling to follow along with the tutor's instructions. Here is the HTML code (index.html) that I have: <!DOCTYPE html> <html lang="en" ...

Determining the Maximum Number of Characters Allowed in a Div Using jQuery

Could anyone provide guidance on how to populate a div with single characters? I want the div to span the width of the viewport. The code to get the width is: $(window).width(); I would like JavaScript to generate HTML similar to this: <div id="text ...