Using a Set after populating it with asynchronous callbacks; for-of loop not accepting .entries() as an Array
Using a Set after populating it with asynchronous callbacks; for-of loop not accepting .entries() as an Array
I am running into issues whenever I try using a Set I have populated asynchronously:
const MaterialType_Requests_FromESI$ = SDE_REACTIONDATA.map(data => this.ESI.ReturnsType_AtId(data.materialTypeID));
let MaterialCollectionSet: Set<string> = new Set<string>();
let MergedMaterialTypes$ = merge(MaterialType_Requests_FromESI$);
MergedMaterialTypes$.subscribe(
MaterialType$ => MaterialType$.subscribe(MaterialType => MaterialCollectionSet.add(MaterialType.name)),
null,
() =>
console.log(MaterialCollectionSet); // Outputs Set object, with a 'size' of 98
console.log(MaterialCollectionSet.size); // Outputs 'size' as 0
);
If I console.log this Set, an Object is returned with a size property of 98, as well as an 'Entities' Array that has each value of the Set I am trying to access...
However, trying to access the size property directly returns a Set size of 0...
Also, the Set.entries() value is NOT being accepted within a for-of loop as an Array, even when using Array.from()...
const MaterialType_Requests_FromESI$ = SDE_REACTIONDATA.map(data => this.ESI.ReturnsType_AtId(data.materialTypeID));
let MaterialCollectionSet: Set<string> = new Set<string>();
let MergedMaterialTypes$ = merge(MaterialType_Requests_FromESI$);
MergedMaterialTypes$.subscribe(
MaterialType$ => MaterialType$.subscribe(MaterialType => MaterialCollectionSet.add(MaterialType.name)),
null,
() =>
// This loop runs zero times, despite Entries having 98 values
for(let entry of Array.from(MaterialCollectionSet.entries()))
console.log(entry)
// console.log(MaterialCollectionSet); // Outputs Set object, with a 'size' of 98
// console.log(MaterialCollectionSet.size); // Outputs 'size' as 0
);
Posts such as this, that say to use for-of loops, suggest this should be working... but it is not.
Where/how am I miss-accessing this Sets properties???
1 Answer
1
Using 'forkJoin', in place of 'merge', solved the issue:
let MaterialType_Requests_FromESI$ = SDE_REACTIONDATA.map(data => this.ESI.ReturnsType_AtId(data.materialTypeID));
let MaterialCollectionSet: Set<string> = new Set<string>();
forkJoin(MaterialType_Requests_FromESI$).subscribe(
MaterialTypes => MaterialTypes.forEach(Type => MaterialCollectionSet.add(Type.name)),
null,
() =>
for(let entry of Array.from(MaterialCollectionSet.entries()))
console.log(entry[0]) // [0] selects the key of the Set, which is the same as the value of the Set at [1]
);
'merge' was requiring me to subscribe to each Observable it had merged together, separately. This means the initial subscription's completion callback was firing before the Set had been populated.
As 'forkJoin' works with the response data directly, the Set is now properly being populated, and can be iterated over with a 'for-of' loop.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy