Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Promises with Observables #453

Open
nhhockeyplayer opened this issue Nov 13, 2021 · 1 comment
Open

replace Promises with Observables #453

nhhockeyplayer opened this issue Nov 13, 2021 · 1 comment

Comments

@nhhockeyplayer
Copy link

nhhockeyplayer commented Nov 13, 2021

Observables were the defacto standard for http in 2016 and now the STANDARD

its nice to see a fixture for nestJS for typegoose

I thought this Promise decision was typegoose related but it is not

typegoose/typegoose#631

Can Promises be ditched for Observables?

  async create(createCatDto: { name: string }): Promise<Cat> {
    const createdCat = new this.catModel(createCatDto);
    return await createdCat.save();
  }

this is what I would like to see returning an Observable to the front end

  async create(createCatDto: { name: string }): Observable<Cat> | Promise<Cat> {
    const createdCat = new this.catModel(createCatDto);
    return await createdCat.save();
  }

Any assist and help is appreciated

can we have Observables please?

@smolinari
Copy link

smolinari commented Nov 14, 2021

No one wants Promises,

Please speak for yourself. I want promises, where they make sense and that is in 99% of TypeScript/ JavaScript code being written. They are an integral part of Node/ JavaScript to make use of JavaScript's event loop in order to stop code from blocking processing.

it all came from functional javascripter's non fluent in Object Oriented modeling and closure camp's and I have seen whole companies bit the dust and go chapt 11 from their widespread impact of chaos async threads and inability to get any of it under control

Not using Promises in TypeScript/JavaScript won't make the code more object oriented. There is no such thing as "async threads" in JavaScript. In fact, Promises or async/ await code is exactly what makes JavaScript so simple to use. Much better than the old days where there were only callbacks.

instead of hardwiring everything onto promises and async/await.

Nothing is hardwired. It's how JavaScript works. Offering Observables is not going to make any JavaScript code more efficient or better.

Let's also take a step back. Observables are part of event management. i.e. certain objects can listen or produce events via working with observables. It's called reactive programming. It is a different paradigm than OOP, whereas OOP can make use of reactivity. What you are asking for is reactive features to Mongoose, which Mongoose itself doesn't support (maybe, more below). Remember, this package (nestjs-typegoose) is a wrapper to get Typegoose working in NestJS and Typegoose is a wrapper for Mongoose. If anything, you should be asking the Mongoose team for this change. And, I'll bet they'll basically ignore it.

Btw, there is a "bit" of reactive programming in Mongoose with "watching".

https://mongoosejs.com/docs/api.html#connection_Connection-watch
https://mongoosejs.com/docs/api.html#model_Model.watch

Or with their hooks system within Middleware:

https://mongoosejs.com/docs/middleware.html

Your ask here is incorrectly placed as mentioned above. And, also instead of thinking you know the solution is to work with Observables, you might want to go to a place like StackOverflow/ StackExchange and present your current challenge and hopefully get people with a lot of knowledge to reply. I'll bet you a beer, they don't end up with saying some package will need to change and offer Observables to solve your problem.

Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants