Move function declaration before reference

The way the example is currently presented requires that the user have an understanding of how Javascript hoists functions. By moving the function declaration before the function reference, the order of the code becomes more clear to the reader, which I think is valuable here. I think it's more important that this material clearly teach the behavior of Svelte, rather than the nuances of Javascript.
pull/5491/head
Daniel Power 5 years ago
parent 377c7647ed
commit 39341bbfdf

@ -1,6 +1,4 @@
<script>
let promise = getRandomNumber();
async function getRandomNumber() {
const res = await fetch(`tutorial/random-number`);
const text = await res.text();
@ -12,6 +10,8 @@
}
}
let promise = getRandomNumber();
function handleClick() {
promise = getRandomNumber();
}
@ -22,4 +22,4 @@
</button>
<!-- replace this element -->
<p>{promise}</p>
<p>{promise}</p>

@ -1,6 +1,4 @@
<script>
let promise = getRandomNumber();
async function getRandomNumber() {
const res = await fetch(`tutorial/random-number`);
const text = await res.text();
@ -11,6 +9,8 @@
throw new Error(text);
}
}
let promise = getRandomNumber();
function handleClick() {
promise = getRandomNumber();
@ -27,4 +27,4 @@
<p>The number is {number}</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
{/await}

Loading…
Cancel
Save