弄乱回调执行顺序

时间:2018-09-12 06:54:14

标签: typescript callback

我在执行此代码时遇到麻烦:

  ngOnInit() {

    let TypeIDSet: Set<number> = new Set();
    let TypeNameSet: Set<string> = new Set();

    SDE_REACTIONDATA.forEach((TypeData: ReactionType_FromSDE) => this.AddTypeID_ToTypeIDSet(TypeData,TypeIDSet));

    TypeIDSet.forEach((id: number) => this.ReturnESI_Type(id, TypeNameSet));

    //TypeIDSet is properly populated
    console.log('ID Set: ' + TypeIDSet);

    // TypeNameSet is empty
    console.log('TypeName Set: ' + TypeNameSet);

  }

  ReturnESI_Type = (id: number, TypeNameSet: Set<string>) => {
    this.ESI.ReturnsType_AtId(id).subscribe((TypeData: ReactionType_FromESI) => this.AddTypeName_ToTypeNameSet(TypeData, TypeNameSet));
  };

  AddTypeName_ToTypeNameSet = (TypeData: ReactionType_FromESI, TypeNameSet: Set<string>) => {
    TypeNameSet.add(TypeData.name);    
  }

  AddTypeID_ToTypeIDSet = (TypeData: ReactionType_FromSDE, TypeIDSet: Set<number>) => {
    TypeIDSet.add(TypeData.typeID);
  };

出于某种原因,尽管TypeIDSet是以相同的方式实现并可以工作的,但TypeNameSet并没有在回调之外持续存在。

0 个答案:

没有答案