I can't figure out if this mechanism that I created is novel or not. I'm guessing that it is not, but I haven't been able to uncover anything by searching that is identical.Some context: I need a promise-like mechanism for notifying "listeners" that some asynchronously loaded object has become available. However, unlike a typical fetch call, this object is loaded with a dynamically loaded script so there is no promise to attach to. Something like an `AsyncSubject` from RxJS is essentially what I need, but I need the mechanism to be as lightweight as possible and without third-party dependencies.What I've devised is a wrapper that combines a promise and a nested generator. At construction, you pass in a generator function that defines a routine that must complete before the promise will resolve (for my scenario this is nothing more than a single yield). Like a promise, it is thenable and like a generator or a subject, you can call `next()`.const generator = function* () { yield; }; const obs = observable(generator); obs.then(() => console.log('Completed'); ) // Will not resolve until generator has completed. obs.next(); // Now promise will resolve. It's sort of an observable or async subject, sort of an event emitter. Maybe some sort of coroutine or a deferrable? Maybe you'd call it a yieldable promise? Does any library out there already implement this? Thoughts?Full source code available here: https://ift.tt/2JCrcBi
Submitted November 04, 2018 at 01:43AM by mattstrom
No comments:
Post a Comment