I am currently working on the following code snippet:
const outputFile = fs.createWriteStream(outputPath);
const requisitionData = this.login().pipe(
map(response => response.data.token),
switchMap(loginToken =>
this.getRequisitions(loginToken).pipe(
expand(response => {
const nextPage = response.data.content.meta.cursor.next;
console.log('Next Page: ' + nextPage);
return nextPage
? this.getRequisitions(loginToken, nextPage ?? undefined)
: EMPTY;
}),
map(response => response.data),
map(requisitions => this.processRequisitions(requisitions, outputFile)),
),
),
);
requisitionData.subscribe();
The functionality of this code is as follows:
- It makes a call to a login endpoint and retrieves the token string
- Using the token string, it calls a "get requisitions" endpoint
- It iterates through the paginated results and calls the subsequent page(s)
- For each page, it saves the result into a file
I want to enhance this code by executing a function once all pages have been traversed, adding a console.log('Process finished');
.
However, my attempts to achieve this (such as adding tap(() => console.log())
in different places) lead to multiple executions.
UPDATE:
I successfully implemented this by modifying the subscribe()
method and passing the callback as the third parameter:
requisitionData.subscribe(
undefined,
undefined,
() => console.log(`Process Finished: Output file: ${outputFile.path}`),
);
Although I still ponder how I can accomplish this within the pipe itself.