2

I have a function returning the promise of an array:

movementListOfTheUserForTheStockCode(userID: string): Promise<calculatedMovement[]>

How to get the length of the array?

How to assign a promising function into a constant?

const  movementList = movementListOfTheUserForTheStockCode(userID);
for (let i = 0; i < rawMovementList.length; i++) {
// do stuff
}
Cem Kaan
  • 2,086
  • 1
  • 24
  • 55

1 Answers1

1

By calling this async method you won't get the actual Array instance, but the Promise of the array. So to get the actual array instance you should await for the promise:

async someAsyncFunction () {
   const  rawMovementList = await rawMovementListOfTheUserForTheStockCode(userID, code);
   for (let i = 0; i < rawMovementList.length; i++) {
      // do stuff
   }
}

or using it by the older way with then approach.

rawMovementListOfTheUserForTheStockCode(userID, code).then(rawMovementList => {
    for (let i = 0; i < rawMovementList.length; i++) {
      // do stuff
   }
})