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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế