How to convert promise<any[]> to any[] : problem with async, await, promise

Viewed 43

I have a problem with async, await and promise. Here is my code block, but I've faced following error.

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

in

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

what is the matter?

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;
  });
}
1 Answers

You need to await markRequired().

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

To answer your last comment, you can only use await inside of an async function, or at the top level (not inside a class or function) with new versions of Node.

markRequired() is not doing anything async, you don't need to make it an async function.

Related