Managing the conversion of a promise<any[]> to any[] can be tricky when dealing with async, await, and promises

I'm having trouble understanding async, await, and promises in my code. When I run the following block of code, I encounter the error message below:

Type 'Promise<any[]>' is missing the following properties from type 'any[]': length, pop, push, concat, and 29 more.

within

objectList = this.markRequired(objectList, requiredList, typeCodeName);

Can anyone help me figure out what's going wrong?

async applyRequiredTypes(objectList: any[], typeCodeName: any, requiredList: any[]) {
  objectList = objectList || [];
  requiredList = requiredList || [];
  objectList = this.markRequired(objectList, requiredList, typeCodeName);
  ...
}

async markRequired(itemList: any[], requiredList: any[], typeCodeName: any) {
  return itemList.map((item: any) => {
    item.required = requiredList.some((requiredTypeCode) => {
      return requiredTypeCode === item[typeCodeName];
    });
    return item;
  });
}

Answer №1

Make sure to wait for the completion of markRequired().

updatedObjectList = await this.markRequired(objectList, requiredList, typeCodeName);

In response to your previous comment, remember that await can only be used within an async function or at the top level (not inside a class or function) in newer versions of Node.

It's worth noting that markRequired() does not involve anything asynchronous, so there is no need to define it as an async function.

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

How to Use Jquery to Input a Date Range in Three Textboxes?

In my form, I have three textboxes, each of which utilizes a datepicker. The purpose is to restrict the user's input based on the dates entered in the previous textboxes. For example, if a user enters a date in the first textbox, the second textbox wi ...

Harmonizing database with AJAX-powered webpage

When using ajax calls to update data on a web page, what is the most effective way to synchronize changes with a database? For example, if I have a comment form that needs to work asynchronously. I write JS code to submit the form data to the database, but ...

Issue with form not being validated

I'm attempting to disable the submit button when a user enters something in the email field, but despite validation, it is not working as expected. What could be the issue with my validation code? function validateEmail(email) { var btnSubmit = ...

"Implementing a drop-down feature for various div elements based on their unique ids

What is the best way to implement dropdown menus in multiple divs with different IDs? I have a user stream where adding a dropdown menu for actions like delete and edit on each post only opens the dropdown in the first post. I know this needs to be handle ...

A secure method for dynamically adding a JavaScript file

In my lang folder, I store language variables for a website. To dynamically include the desired file based on the user's language selection, I use session variables. For example, if the user selects English, 'en' is stored in the lang variab ...

Why are Javascript variables not producing the desired result? It seems like they are being treated as strings instead

Why is there an error? Expected result: 1360000 Actual result: 1000000360000 <script> function calculateTotal() { var amount = document.getElementById("borrow").value; var duration = document.getElementById("return").value; var interest = durati ...

Tips for eliminating the empty element from a JavaScript array

Here is the code implementation I am working with: Array.prototype.abc = function(condition, t){ var arr = []; for( var i = 0; i < this.length; i++){ arr.push(condition(this[i],t)); } return arr; }; var a = [1,2,3,4]; ...

Initiate an AJAX request exactly on the 20th second of each minute

I have a piece of code where I am utilizing AJAX to make a call. My goal is to trigger the AJAX call every 20th second of each minute. Below is the AJAX request that I am executing. setInterval(function(){ $.ajax({ url: url, headers: { ...

SVG to PDF conversion is successful with Plotly.js, however, C3.js is currently experiencing issues with this

After an onclick event, I am using css2pdf to save my svg chart to pdf. It functions perfectly with plotly.js, but unfortunately not with c3.js - you can check out my fiddle here: http://jsfiddle.net/3hs7cs4f/ I suspect the issue lies within the c3.js svg ...

Enforce linting rules for webpack aliases using ESLint

I am currently working with a webpack configuration file that functions as a factory (react-universally boilerplate). In this setup, I have included an resolve option structured like so: resolve: { // These extensions are attempted when resolving a ...

Argument typed with rest properties in Typescript objects

I'm relatively new to Typescript and have managed to add typings to about 90% of my codebase. However, I'm struggling with rest/spread operators. While going through our code today (which I didn't write), I came across this snippet that does ...

What is the most efficient way to check for the presence and truthiness of a nested boolean in Node.js?

There are instances where I must verify the deeply nested boolean value of an object to determine its existence and whether it is set to true or false. For instance, I need to ascertain if payload.options.save is assigned a value of false, yet I am uncert ...

Aligning a div next to a text field in line

After numerous attempts, I am still struggling to align the 'add' button inline with my text field. The icon needs to be positioned correctly as shown below. I have experimented with grids and various other methods, but it refuses to cooperate. ...

Retrieve all documents from a Firebase User's collection

Recently, I created a Firebase cloud function where my goal is to access every user's internal collection named 'numbers' and examine each document within that collection for some comparisons. Do you have any insights on how I can achieve t ...

Encounter a snag when attempting to upgrade to React version 16.10.2 while working with Ant Design Components - the error message reads: "

After upgrading to the latest React version 16.10.2, I encountered issues while using Ant Design Components. I specifically wanted to utilize the Title component from Typography. Here is an example of what I was trying to do: import { Typography } from & ...

What is the method used by React's functional setState to identify which fields have been changed?

I've discovered that when using an object as a react state (let [state, setState] = useState({})), and updating it like this: setState(s => { s.a = 42; return s; }) The components depending on s.a do not get re-rendered. However, if you u ...

Authentication failed due to Bcrypt.compare() returning invalid credentials

const express = require('express'); const router = express.Router(); const auth = require('../../middleware/auth'); const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const config = require(&apo ...

What is preventing my textbox from accepting decimal points?

After applying this code, I encountered an issue where the textbox does not accept input with a decimal point .. <asp:TextBox ID="txtDetAmount" runat="server" AutoPostBack="true" OnTextChanged="OntxtDetAmountC ...

Facing an issue with Axios in React Native where I am unable to POST a Blob or File

After capturing an image with react-native-image-picker, I am attempting to upload the raw data of the picture using Axios. I managed to create a blob by fetching the image URI and converting it like so: const file = await fetch(response.uri); const theBl ...

Can you explain the concept of Object Buffer in Node.js?

While working with nodejs, I encountered the following code snippet: const fileToUpload = fs.readFileSync(test_file_path); console.log("fileToUpload: type: ", typeof fileToUpload, ", content: ", fileToUpload); and the output was: t ...