It is not possible to use an async function with Ionic 4 ToastController buttons

Incorporating a new function into the handler of my ToastController button to return a promise (in this case: this.navCtrl.navigateForward()) is something I need assistance with.

This is what my code looks like:

const toast = await this.toastController.create({
  header: header,
  message: message,
  position: 'bottom',
  animated: true,
  buttons: [
    {
      side: 'start',
      text: 'Action',
      handler: () => {
        this.navCtrl.navigateForward('/destination');
      }
    }
  ]
});
await toast.present();

The issue arises when I receive the notification:

Promises must be handled appropriately (no-floating-promises)
I wish to make the following adjustment:

const toast = await this.toastController.create({
  header: header,
  message: message,
  position: 'bottom',
  animated: true,
  buttons: [
    {
      side: 'start',
      text: 'Action',
      handler: async () => {
        await this.navCtrl.navigateForward('/destination');
      }
    }
  ]
});
await toast.present();

However, an unhelpful error message pops up:

Type '{ side: string; text: string; handler: () => Promise<void>; }' is not assignable to type 'string | ToastButton'.
  Type '{ side: string; text: string; handler: () => Promise<void>; }' is not assignable to type 'ToastButton'.
    Types of property 'side' are incompatible.
      Type 'string' is not assignable to type '"start" | "end"'.

I have been trying to solve this problem but seem to be stuck. Any guidance on where I might be going wrong would be greatly appreciated. Thank you!

Answer №1

If you find yourself in a situation where you need to wait for a page transition before executing some code, why not consider using the .then approach?

const notification = await this.notificationController.create({
  header: header,
  message: message,
  position: 'bottom',
  animated: true,
  buttons: [
    {
      side: 'start',
      text: 'Action',
      handler: () => {
        this.navCtrl.navigateForward('/destination').then(()=>{
            // Perform actions after the page transition here
        })
      }
    }
  ]
});
await notification.present();

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

Is it more efficient to define a variable or call a method from an object?

Which approach is more effective and why? Option 1: Declaring a variable exampleFunction(requestData: Object) { const username = requestData.username; doSomething(username); } Option 2: Accessing the object property directly exampleFunction(reques ...

Upon completing the installation of the @angular/cli@latest node module, the ng file was unexpectedly missing

I'm currently working on Git Bash on my Windows 10 machine. The command I used was: npm install --save @angular/cli@latest Here is the result: + @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad9d6d3fa8b899488 ...

I'm diving into the world of Typescript and trying to figure out how to use tooltips for my d3 stacked bar chart. Any guidance on implementing mouseover effects in Typescript would be greatly

I am currently facing some issues with the code below and need guidance on how to proceed. I am new to this and unsure of how to call createtooltip. Any assistance would be greatly appreciated. The error message states that createtooltip is declared but n ...

Inject a cookie into the Axios interceptor for the request handler

I am in the process of setting up Axios to always include a request header Authorization with a value from the user's cookie. Here is my code: import axios, { AxiosRequestConfig, AxiosResponse} from 'axios'; import {useCookies} from "react-c ...

I am unable to add a new property to the request object in the Express framework

My goal is to add a new property to the request object in typescript. Here's the code snippet I'm using: import { request, Request, response, Response } from "express"; ((req: Request, res: Response) => { console.log(req.user); ...

Having trouble with triggers: Unable to locate the module 'csv-parse/sync' for parsing

Currently, I am utilizing Firebase functions to create an API that is capable of parsing CSV files. However, whenever I attempt to utilize csv-parse/sync instead of csv-parse, my deployment to Firebase Functions encounters a failure with the subsequent er ...

What is the best method for managing an event loop during nested or recursive calculations?

When it comes to breaking a computation and releasing using setTimeout(), most examples seen involve having a shallow call stack. But what about scenarios where the computation is deeply nested or mutually-recursive, like in a tree search, with plenty of c ...

Retrieve an established SQS eventSource in AWS CDK

When working with AWS CDK, there is a built-in function called addEventSource that allows you to easily add new SQS triggers (eventSources) to a lambda function. However, I'm curious if there is a way to access and modify the existing eventSources ass ...

Storing browser cookies in Azure Static Web App

I recently set up a solution on Azure with a web API in .NET 6 deployed to App Service, and a front-end application in Angular deployed to Azure Static Web App. I made sure to configure CORS on the back-end App Service to allow the front-end origin. Howeve ...

Replace a portion of text with a RxJS countdown timer

I am currently working on integrating a countdown timer using rxjs in my angular 12 project. Here is what I have in my typescript file: let timeLeft$ = interval(1000).pipe( map(x => this.calcTimeDiff(orderCutOffTime)), shareReplay(1) ); The calcTim ...

Having trouble with installing npm package from gitlab registry

I recently uploaded my npm package to the GitLab package registry. While the upload seemed successful, I am facing an issue trying to install the package in another project. When I run npm install, I encounter the following error: PS E:\faq\medu ...

Changing a string into a date format with the help of JavaScript AngularJS

My string is as follows: 13-12-2017 05:05 AM I am looking to convert it to the following format: Date 2017-12-13T05:05:00.000Z Attempted Solution: var mydate = '13-12-2017 05:05 AM'; var selectedDate = new Date(mydate); Upon logging the selec ...

Navigating directly to URLs in Angular Universal with iisnode

My issue involves an Angular Universal application. While everything runs smoothly locally with express, and inside of node, deploying the production build to IIS with iisnode results in a 500 Internal Server Error when navigating directly via URL. Unfor ...

Angular9 displays a variable as undefined

As a newcomer to Angular 9, I have been encountering some issues lately. Specifically, while using Ionic 5 with Angular 9, I am facing an error where a variable appears as undefined when I try to console.log it. Please bear with me as I explain the situati ...

Is there a way to append a unique property with varying values to an array of objects in TypeScript?

For instance: items: object[] = [ {name: 'Backpack', weight: 2.5}, {name: 'Flashlight', weight: 0.8}, {name: 'Map', weight: 0.3} ]; I prefer the items to have an 'age' property as well: it ...

What is the process for finding GitHub users with a specific string in their name by utilizing the GitHub API

I'm looking to retrieve a list of users whose usernames contain the specific string I provide in my query. The only method I currently know to access user information is through another endpoint provided by the GitHub API, which unfortunately limits t ...

Confirming the HTML5 input type of numerical values

I have a requirement to display 2 different validation messages for the input type shown below <input type="number" step="1" formControlName="Ordinal" /> Within my reactive forms setup, I have a formGroup structured like this formBuilder.group({ ...

Error 401 encountered while accessing the Binance API using Ionic and Angular

function makePrivateCall(apiSecret, apiKey, endpoint, data = null, isGetRequest = true) { const timestamp = Date.now(); const recvWindow = 60000; //maximum allowed, default 5000 var obj = { apiSecret, ...data, timestamp, ...

Ensure that the value of a variable in the Ionic/Angular template has been properly initialized

I am currently facing an issue: I have an array of blog posts. Some of them have photos, while others do not. I aim to display the first photo if any are set. How can I verify whether the URL value in my array is set? <ion-header> <ion-navbar& ...

Angular 10 does not fulfill promises as expected

In the Angular 10 project I'm working on, I encountered an issue while trying to call an API request asynchronously using promises. The code I wrote didn't seem to execute the API call as expected and kept exiting at the first line without progre ...