在一系列回报之后完成一个可观察的

时间:2019-08-27 19:23:07

标签: angular

我有一个可观察的对象,一个接一个地在其中运行/调用promise,我想在forEach()完成后完成它。现在,.complete()发生在成功处理foreach的对象之前。

 return new Observable<Person>(observer => {
 //get list of Person object from indexdb first
 // then loop over each
 this.service.getMembers().subscribe(people=>{
//process each person
 people.forEach(person=>{
 //promise one
  this.service.process1(person).then(processOne=>{
     // person object now have some values/properties added to it
   return processOne

    }).then(person=>{
      //person from processOne
      // do something to it now and emit it
      console.log(person); // i see correct data here for each person
      observer.next(person)


     })


    );

     observer.complete(); // doesn't work as it reaches here immediately

   });

 });

1 个答案:

答案 0 :(得分:0)

您可以make: Entering directory '/.../Delphes-3.4.1' >> Generating tmp/classes/ClassesDict.cc make: rootcint: Command not found Makefile:2258: recipe for target 'tmp/classes/ClassesDict.cc' failed make: *** [tmp/classes/ClassesDict.cc] Error 127 make: Leaving directory '/content/Delphes-3.4.1' 与诺言。全部

complete

清洁剂溶液

    const promisses: Array<Promise> = [];
    people.forEach(person=>{

      promisses.push( 
           this.service.process1(person).then(processOne=>{
               return processOne    
           }).then(person=>{
               observer.next(person)
           });
      );
   });
   Promise.all(promisses).then(() => observer.complete())
async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}