Is it true that Async methods in Typescript return promises?

I'm currently in the process of familiarizing myself with Async/Await in Typescript.

I've been updating existing code, for example:

getImportanceTypes(): Promise<void> {
        return this.importanceTypeService.list()
            .then(items => {
                this.importanceTypes = items;
            });
}

which is being converted to:

async getImportanceTypes(): Promise<void> {
     this.importanceTypes = await this.importanceTypeService.list()
}

My question now is: Does this new implementation really return a promise? It seems to compile successfully, but it looks like the code execution pauses at the await until it finishes, and then continues.

I'm asking because I have around 10 similar calls to the one mentioned above (for different type tables) and I want them to run concurrently using Promise.all.

Answer №1

Absolutely, async functions do indeed return promises in both JavaScript and TypeScript. The usage of async/await is essentially a more convenient way to work with promises, making them easier to create and handle (while still being incredibly useful).

Your approach to specifying the return type is correct. There has been some debate on this topic, as some argue that when a function is declared as async, it should be enough to specify its resolution type without explicitly mentioning the promise itself. However, for now, we still use Promise<x> instead of just x.

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

Show a pop-up modal when a valid selection option is made

I am using jQuery to create a pop-up modal when a selection is made in my dropdown menu. However, I am having trouble preventing the pop-up from appearing when the default option (which has no value) is selected. This is my HTML: <div class="dropdownP ...

Tips on determining the type of DOM element for CSS Selector adjustment

In this case, $self is not returning the expected result and I am getting "undefined". To adjust the CSS selector to correctly target the desired element, I need to determine which element type I am dealing with. Is it the "ul", "li", or "a" element? Whil ...

Guide on determining if a div contains child elements in React JS

I was working on creating a blog with user comments, and I organized all the responses in a parent div. However, I needed to display a message when there were no comments present. To achieve this, I wanted to determine if the parent div had any children or ...

Discover the steps to eliminate an element's attribute with the help of mutationObserver and mutationrecord

In an effort to remove attributes added by a 3rd party library from my webapp at runtime, I have been using the code provided below: document.addEventListener('DOMNodeInserted', () => { const elements = document.querySelectorAll('[aria- ...

Problems with updating HTML/Typescript in Visual Studio and Chrome are causing frustration

While working on my company's application locally and making HTML/TS changes, I am facing an issue. Whenever I save/hot reload and refresh the browser, no changes seem to take effect. I've tried stopping debugging, building/rebuilding, and runni ...

Update CSS transparency using PHP

My project involves a heart icon that changes appearance when hovered over, fading into a full heart instead of just an outline. If the user has already liked the heart, it should display as solid pink by default. I planned on checking if the user had like ...

How to display information from a JSON file using dynamic routing in a React.js application

I'm currently working on a project to replicate Netflix using reactjs, but I've hit a roadblock and can't figure out what to do next. I've tried watching YouTube tutorials and reading articles online, but I haven't been able to fin ...

Removing a comma from a value in Vue.js: Step-by-step guide

I am currently facing a challenge with removing a comma from a value using Vue.js. Here is the code I am working with: displayValue(){ var displayValue = this.value; console.log(displayValue); if(displayvalue contains a comma){ display ...

The issue with updating state in React using dispatch within the same method is not working

Initially, I had a situation where the state was updating correctly with two separate methods (onClick). const sizesMeasureChoise = useSelector(getSizesMeasureChoise); const chooseMeasure = (measure) => { dispatch(setSizeMeasureChoise(measure)); ...

Is it possible to omit certain columns when extracting data from a Kendo grid?

My challenge involves extracting data from a Kendo grid using the following JavaScript call: var data = JSON.stringify($(".k-grid").data("kendoGrid").dataSource.data()) This retrieves all properties in the C# class for the records. There are three proper ...

Tips for populating all the ionic form fields using speech recognition technology

In the process of developing an Ionic 4 application, I am faced with a challenge where I need to fill in multiple form fields using the Ionic speech-recognition plugin. Currently, I am only able to populate one field at a time. What I am looking for is a w ...

There are a couple of issues here: specifically, the `this.myMethod` function is not recognized as a function, and the click event added by the `addEventListener` function

I am currently attempting to create a timer using vue.js, but I am encountering some issues with my methods section: methods: { saveRunningMethod() { var runningData = { duration: `${this.hour} : ${this.minute} : ${this.se ...

The process of loading an image becomes stuck once the submit button is pressed

I have multiple JSP pages where I use an ajax call to submit data. On these pages, I am able to display a loading image before the ajax call and hide it once the call is complete, which works perfectly. $(document).ajaxComplete(function() { $("#loadi ...

Prepending a string to the key of a JSON object using JavaScript

Is there a way to add 'd:' before the key of a JSON object? Here is the JSON data: "data": { "aa": "value", "ab": "value" } The expected result should look like this: "d:data": ...

Prevent users from adding or removing any letters

Exploring the ACE Editor integration with AngularJS (utilizing UI-Ace). I have a question. Is it possible to limit the user's actions to: Only allow entering a predefined character (for example, ;) Prevent deletion of any characters except for the p ...

AngularJS is patiently anticipating the population of the scope for ng-show

I have implemented ng-show to display an error message based on an array. The code I used is ng-show="!items.length". However, since the items are populated after a request, there is a flickering effect for a brief moment before the items are loaded. Is th ...

Persisting a modal in NextJS on page refresh - navigating with elegance

After reviewing the Modal method discussed on this particular page in the NextJS documentation, as well as exploring responses to questions on Stack Overflow like this one, I noticed that in all instances, when the page is refreshed, the modal simply displ ...

Having trouble configuring bcryptjs in a React.js project

I have been working on setting up a single registration page within a react component. However, I encountered an issue when trying to hash the password before sending the response to my back-end. I am puzzled as to why the code snippet below is not functi ...

Leverage classes from a CommonJS module within a Typescript environment

I am facing an issue with my npm package setup. Here is a snippet from one of the files: 'use strict' module.exports = class TModel { constructor (app) { this.app = app } static schema () { } } I am trying to incorporate this int ...

Learn how to restrict input to only specific characters in an input box using Angular2 and validations

Is it possible to restrict user input in an input box to only specific characters such as '7' and '8'? I tried adding validations with attributes like type="number", min="7" and max="8", but even then other keys can be inserted before v ...