Skip to content

Instantly share code, notes, and snippets.

@dimitardanailov
Created September 10, 2018 09:55
Show Gist options
  • Save dimitardanailov/96a36b073b7a4e385f82b206d6862726 to your computer and use it in GitHub Desktop.
Save dimitardanailov/96a36b073b7a4e385f82b206d6862726 to your computer and use it in GitHub Desktop.
Example: Working with MongoDB Streams
@Get('/hello-world-streams')
async streams() {
// Get Mongodb cursor
const cursor = this.repository.getCursorToAllRecords();
const promise = new Promise((resolve, reject) => {
cursor.on('data', doc => {
console.log(doc);
});
cursor.on('close', () => {
resolve();
});
cursor.on('error', error => {
reject(error);
});
});
return await promise.then(() => {
return 'Stream was closed';
}).catch(_error => {
return 'Ops, something is wrong';
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment