4 ms·
I've been toying with the idea of promise free async/await lately. I'm interested in feedback anyone would have. https://github.com/bessiambre/casync https://g
by BenoitEssiambre 6y ago
I've been toying with the idea of promise free async/await lately. I'm interested in feedback anyone would have.
https://github.com/bessiambre/casync https://github.com/bessiambre/casync
- no_wizard 6y agoSo generators are used here because of the natural way of calling “next” and yielding a result. How would this work for say, an XMLHttpRequest? Since you have to next into callbacks there I don’t think yield travels the function boundary...so you abuse next instead?
- BenoitEssiambre 6y agoWow I haven't used that directly in a long time. Ideally you would wrap it to be in continuation passing style. This would look something like: function fetchUrl(url,done){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 400)) { done(null,xhr.responseText); }else{ done(new Error("something went wrong")) } } } xhr.open('GET', url, true); xhr.send(null); } Then you could do: let response=yield fetchUrl('http://example.com',next); edit: In case this doesn't really answer the question: If you really don't want to wrap, it would work inline too. It would just be harder to read and follow: var someFunction =casync(function*(url,next){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 400)) { next(null,xhr.responseText); }else{ next(new Error("something went wrong")) } } } xhr.open('GET', url, true); var response = yield xhr.send(null); ... });
- fenwick67 6y agoI have avoided yield() and generators like the plague because they're hard to read and I've never seen them used except in examples explaining how they work. Yield feels like recursion - a neat trick for some corner cases but generally unnecessary.
- BenoitEssiambre 6y agoThis is how async/await is implemented. I think 'await' is almost just a synonym for yield. I didn't invent that part. I just made a faster, better encapsulated, promise free version.
- Kwantuum 6y agoI'm failing to see how async/await has more promises than your implementation.
- BenoitEssiambre 6y agoAsync/await requires passing a promise to await. If you read the documentation for async/await, it starts with: "These features basically act as syntactic sugar on top of promises": https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await https://developer.mozilla.org/en-US/docs/Learn/JavaScript/As...
- galaxyLogic 6y agoJavaScript syntax for generators is clunky no doubt. But they let you do some things more naturally than anything else in JavaScript. They keep track of state "implicitly" you don't have to do an assignment to 'save a state", you just yield and your state is remembered. I found generators useful for implementing my home-brew parsers. Parsers must try out all possible combinations of possible syntactic elements before they can say that parse failed. Generators are a good tool for that
- alcover 6y ago> Parser in JS Insteresting ! Would you mind sharing how you did ?
- galaxyLogic 6y agoThe basic idea is that a generator yields one potential parse of a section of input-string. The caller continues checking the rest of the string but if it fails to find all expected syntactic elements it will go back to the generator basically asking it: Your previous suggestion did not work out, can you give me another possibility?