openid / AppAuth-JS

JavaScript client SDK for communicating with OAuth 2.0 and OpenID Connect providers.
Apache License 2.0
977 stars 162 forks source link

async/await vs event emitter #30

Closed stupiduglyfool closed 6 years ago

stupiduglyfool commented 6 years ago

I wasn't sure if I should open another issue for this, but I have been experimenting with replacing the event emitter with promises..

https://github.com/stupiduglyfool/AppAuth-JS/tree/feature/asyncawait

I have temporarily removed extraneous code from the repo whilst I demo the idea..

When I build my application based on this it feels a lot more intuitive and easier to wire up.. async request/reply methods, rather than subscribing to events, then making a request..

example usage:

https://gist.github.com/stupiduglyfool/cfdadb7005ef548f79d91227ee00c89a#file-async-await-consumer-example

updated electron async/await handler: https://gist.github.com/stupiduglyfool/42197f96f93584b5fdccdca7fc90f785#file-electron_request_handler-ts

What do you think?

tikurahul commented 6 years ago

I prefer the async, await pattern vs. the EventEmitter version. However there are a couple of things to keep in mind.

When working with Electron (you are working with Node 7.1.x) which supports async await natively. Also given the way redirect requests are being intercepted, you are in effect preventing a full page reload. Thus the state machine based async await approach works extremely well for the Electron/Node use case.

Once of the other use-cases is the use of this library in a Web app context. Given that the emitted JS still targets ES5, I cannot assume async await support. I hope this makes sense.

Do you want to create a new sample GitHub repo, which I can link to from my sample. I feel like a lot* of the modifications you made, are very useful for others.

stupiduglyfool commented 6 years ago

Thanks, from what I have seen, the typescript transpiler deals with this sort of problem, you can write async/await in your typescript and it transpiles into promise based code..

tikurahul commented 6 years ago

Yeah. I wish everyone just used TypeScript sometimes. 😀