我有一个可观察的对象,一个接一个地在其中运行/调用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
});
});
答案 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);
}
}