简体   繁体   中英

How can I get object at certain index position from Promise?

I have a method that returns a Promise of tuple that includes an array of regions and the count (before filtering) from the server.

getMany(getManyRegionsQuery: GetManyRegionsQuery): Promise<[Region[], number]>

I have a variable where I am only interested in the regions.

regions: Promise<Region[]>;

Is it possible to get just the regions array of the Promise tuple?

Something like this (which obviously does not work)

this.regions = this.regionsStore.getMany(new GetManyRegionsQuery())[0];

Can I in some way tell the compiler that I only want the object at index position 0 from the Promise?

Use then :

this.regionsStore.getMany(new GetManyRegionsQuery())
    .then(([regions,]) => regions)

Or equivalent:

this.regionsStore.getMany(new GetManyRegionsQuery())
    .then(result => result[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM