derbyjs / racer

Realtime model synchronization engine for Node.js
1.18k stars 116 forks source link

Added async verions of most Model and Query methods #275

Closed IAkumaI closed 2 months ago

IAkumaI commented 4 years ago

Hi. I added async versions (it returns Promise) of most methods of Model and Query. And created some tests for it.

If method returns some value it will be passed to resolve() All methods has name {original}Async. So, now you can write code with async/await

What do you think about moveing to promise-based racer?

IAkumaI commented 4 years ago

Any reply?

ericyhwang commented 4 years ago

Hello - Thanks for the implementation and tests! It took me some time to find time to discuss this with Nate.

We do eventually want to move Racer to a Promise-based API, but we want to take it slower. Once it's in the public API, we have to much more careful to not cause breaking changes, without a new major version.

We'd be OK for now with adding Promise versions of fetch, subscribe, and whenNothingPending.

However, for the mutator methods, we want to think some more about what the APIs would look like, since many of them currently have return values. Such as model.set() returning the previous value set at that path.

The nice thing is, if you want to do this today, you can still do the augmentation of Model.prototype from your own code.

IAkumaI commented 4 years ago

Thanks for the reply. What is wrong with async mutators with return values? Async functions can return values as well as normal functions. I do not see the problem here.

In current implementation it just call resolve(result);, so you can do this: let prevValue = await model.setAsync('path', 'new value');

In fact this PR is just promisify for callback-methods. Better if you will rewrite orifinal methods, but, yes, this will break current API (it is the reason why I didn't it).

craigbeck commented 2 months ago

Async methods were added with Racer v2