I've written this function for an Angular app that is intended to get the contents of an Observable this.lines$
and return it as an array within an object. It seems to work but I wonder if there could be occasions when it won't?:
public getOutput(): { lines: Line[] } {
const linesArray: Line[] = [];
this.lines$
.pipe(
map(lines => {
lines.forEach(line =>
lines.push({
lineNumber: line.lineNumber,
variables: line.variables,
})
);
}),
take(1)
)
.subscribe();
return {
lines: linesArray
};
}
My main concern is that the return at the end of the function could happen before the contents of the Observable's subscription are run. Could that be the case? Is there a better way of achieving this?