3 ms·
> Luckily after a migration that has taken many years, now most of the libraries use async/await instead of callbacks. Is that true? I was looking to port one
by wvenable 6y ago
> Luckily after a migration that has taken many years, now most of the libraries use async/await instead of callbacks.
Is that true? I was looking to port one of my C# projects to TypeScript/Node and it uses gRPC and it's not async/await compatible. That's a pretty major library in my opinion.
In the end I didn't end up porting my project because the lack of elegance compared to the C# version was too depressing -- the C# code made heavy use of async and porting without that was just too messy. I'll take another shot at the project once this problem is totally solved.
- yawaramin 6y agoWell, it's true for libraries that were using Node-style callbacks ('nodebacks') which had an upgrade path to promises[1] and from there to async/await. If some library was doing a completely different style of async, then unfortunately it won't get that easy migration path but it can probably still use the Promise constructor[2] to wrap the async operation in a promise. [1] https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_... [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- status_quo69 6y agoYou can usually promisify the functions that take a callback via util.promisify (although I've done it manually myself as well).
- shp0ngle 6y agoactually not the gRPC functions, since they have options argument with `onEnd` key with callback. So you need to promisify those manually.
- throwanem 6y agoI don't know which library you were looking at, but if it wasn't https://github.com/protobufjs/protobuf.js https://github.com/protobufjs/protobuf.js, it probably should be. That one uses promises, so is async/await compatible (JS async/await is just sugar over promises) and I've seen it work well enough in production use to have no reservations in recommending it.
- wvenable 6y agoI googled "grpc typescript" and worked through the first few pages of links. That particular library never came up. I'll take a look.
- franciscop 6y agoYes, I said most and not all, there are still some older libraries that haven't been updated. These are fairly rare, and you can still util.promisify, but sure it's definitely not ideal. Please note that not all callbacks can (or should!) be converted to promises, e.g. events like `on('click', ...)` don't have a clear way to be promisified since a promise can only be resolved once and stays resolved.
- AbuAssar 6y agoobservables come handy in such cases