0

I have A LOT of promises with some errors especially where you use resolve. I want the two blocks to be Async, One after another, but Its not exactly working. Is this too many promises? Am I implementing them right?

   SetRoom(){
    return new Promise((resolve,reject) =>{
          var prom1= new Promise((resolve)=>{
            console.log("attempting RoomProm1");
            this.RoomLength=this.af.database.list('/Rooms');
            this.RoomLength.take(1).subscribe(snap=>
              {this.rLen=snap.length; resolve(true); });
          });
          prom1.then(_=>{
            this.RoomLength.subscribe((snap)=>{this.RoomList = snap[this.rLen];
            this.Room=this.af.database.list(`/Rooms/${this.RoomList.RoomNum}`);
          });

            prom1.then(_=>{
              this.RoomKey=this.RoomList.RoomNum;
              console.log('Key = ', this.RoomKey);

              return resolve(true);
          });
      });
    }
Ahad Cove
  • 323
  • 3
  • 12
  • 1
    Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Dec 29 '16 at 20:16
  • How often does `subscribe` fire its callback? If it's multiple times, then promises are no option anyway (use rxjs instead). And how does it signal errors? – Bergi Dec 29 '16 at 20:20
  • It fires multiple times. hmm so just stick with rxjs how exactly would I go about that? – Ahad Cove Dec 29 '16 at 20:25
  • I have no clue what you are trying to accomplish. Do not append multiple `.thens()` to the same promise and use global state. Return an output of your promise using `resolve(myValue)` which then can be used as input `.then(myValue => ...)`. Try to apply this to your code and see where that brings you – Mark van Straten Dec 29 '16 at 21:12
  • So Should I make separate functions for each async operation I want with their own separate promise returns? – Ahad Cove Dec 29 '16 at 21:16

0 Answers0