-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CS2] Async/await documentation; fix for Underscore templating #4351
Conversation
When documentation is updated, only index.html.js is usually updated. Compilation is done when releasing. This makes review easier. |
@lydell OK, how about now? l kept the |
through the <code>yield</code> keyword. There's no <code>function*(){}</code> | ||
CoffeeScript also supports | ||
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*">generator functions</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async functions</a> | ||
through the <code>yield</code> and <code>await</code> keywords respectively. There's no <code>function*(){}</code> or <code>async function(){}</code> | ||
nonsense — a generator in CoffeeScript is simply a function that yields. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a generator in CoffeeScript is simply a function that yields, and an async function in CoffeeScript is simply a function that awaits.
And then remove the Likewise
line below.
@@ -184,10 +184,10 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit | |||
|
|||
do renderIndex = -> | |||
codeSnippetCounter = 0 | |||
rendered = _.template fs.readFileSync(source, 'utf-8'), | |||
render = _.template fs.readFileSync(source, 'utf-8') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What did you fix here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newer Underscore has a different templating API.
Do we want the example to be something real that people could run in their browser consoles? Like the first example in this comment? But it would have to be rewritten to not be generic . . . maybe change Whether we use that example or something else, because each example block has a |
@GeoffreyBooth That might be a good idea, but the sample won't run in most browsers today. How about sleep = (ms) ->
new Promise (resolve) ->
window.setTimeout resolve, ms
window.countdown = (seconds) ->
for i in [seconds..1]
alert "#{i} second(s) to go..."
await sleep(1000) # wait one second
alert "done!" Then we could just run |
This is good. I think I like the |
Yeah, this is a better example. We should change the |
Meh. Other examples use |
The problem with I see your point about sleep = (ms) ->
new Promise (resolve) ->
window.setTimeout resolve, ms
window.countdown = (seconds) ->
document.body.innerHTML += '<aside id="countdown"
style="position: fixed; top: 50%; left: 0; right: 0;
text-align: center; font-size: 75vh"></aside>'
for i in [seconds..1]
document.getElementById('countdown').innerHTML = i
await sleep(1000) # wait one second
document.body.removeChild document.getElementById('countdown') # done! You can run it via the console in Chrome Canary: var sleep;
sleep = function(ms) {
return new Promise(function(resolve) {
return window.setTimeout(resolve, ms);
});
};
window.countdown = async function(seconds) {
var i, j, ref;
document.body.innerHTML += '<aside id="countdown" style="position: fixed; top: 50%; left: 0; right: 0; text-align: center; font-size: 75vh"></aside>';
for (i = j = ref = seconds; ref <= 1 ? j <= 1 : j >= 1; i = ref <= 1 ? ++j : --j) {
document.getElementById('countdown').innerHTML = i;
await sleep(1000);
}
return document.body.removeChild(document.getElementById('countdown'));
};
countdown(3); |
@GeoffreyBooth I have the same issue, but your example seems a bit too complex. How about sleep = (ms) ->
new Promise (resolve) ->
window.setTimeout resolve, ms
window.countdown = (seconds) ->
if not window.speechSynthesis?
alert('speech API not supported in your browser')
return
for i in [seconds..1]
utterance = new SpeechSynthesisUtterance("#{i}")
speechSynthesis.speak(utterance)
await sleep(1000) # wait one second
alert "done!" ? |
Good point about sleep = (ms) ->
new Promise (resolve) ->
window.setTimeout resolve, ms
window.countdown = (seconds) ->
for i in [seconds..1]
if window.speechSynthesis?
utterance = new SpeechSynthesisUtterance "#{i}"
window.speechSynthesis.speak utterance
console.log i
await sleep 1000 # wait one second
alert "Done! (Check the console!)" |
Wow, I thought the speech synthesis thing was a joke. Yeah, that’s a pretty cool example. Is it worth even checking for support? It’s supported in the one browser where async/await is supported (Chrome Canary). And we’re not checking for async/await support, though we could. But I gather from your dislike of the DOM element approach that you’re aiming for the most minimal example possible? How about this? sleep = (ms) ->
new Promise (resolve) ->
window.setTimeout resolve, ms
countdown = (seconds) ->
for i in [seconds..1]
window.speechSynthesis.cancel() # cancel any prior utterances
window.speechSynthesis.speak new SpeechSynthesisUtterance "#{i}"
await sleep 1000 # wait one second
window.speechSynthesis.speak new SpeechSynthesisUtterance 'Blastoff!'
countdown(3) No |
That’s also good. The only thing I worry about is if Firefox starts supporting |
@GeoffreyBooth That works, but to simplify things I added say = (text) ->
window.speechSynthesis.cancel() # cancel any prior utterances
window.speechSynthesis.speak new SpeechSynthesisUtterance text |
Thanks @GabrielRatener, the current version looks great. I’m happy with it as is, but do we want to add some kind of compatibility check? Speaking of Firefox, apparently a few days ago it was added to their nightly build: https://blog.nightly.mozilla.org/2016/11/01/async-await-support-in-firefox/ That page has a pretty cool example and demo, too. I just checked in Firefox, and speech synthesis is already supported. caniuse.com says that a flag needs to be set, but it was enabled by default for me. I’ve been trying to find a way to test for async/await support, and unfortunately the only ways to do so either involve loading an external file (like in the Mozilla demo) or using the dreaded try
eval "(async function() {
return 10 + (await Promise.resolve(10));
})() instanceof Promise"
asyncSupported = yes
catch error
asyncSupported = no I gather we probably don’t want to put an |
@GeoffreyBooth That seems really messy. Maybe I can just add a comment about browser compatibility for now. How about sleep = (ms) ->
new Promise (resolve) ->
window.setTimeout resolve, ms
say = (text) ->
window.speechSynthesis.cancel() # cancel any prior utterances
window.speechSynthesis.speak new SpeechSynthesisUtterance text
# works only in browsers supporting ES async/await
countdown = (seconds) ->
for i in [seconds..1]
say "#{i}"
await sleep 1000 # wait one second
say "Blastoff!"
countdown(3) ? |
@GabrielRatener Maybe put the comment at the top, and mention that the browser also needs to support speech synthesis? Also I think you can just do |
Added async function documentation as suggested by @GeoffreyBooth, and fixed a documentation generation bug.