Random Coding Notes

My TL;DR on JavaScript async/await. From the top…

JavaScript is a single threaded language, our code runs synchronously in a single thread. Asynchronous behaviour can be modelled using callbacks, functions that are created to be executed at a later point in time, typically when a blocking operation has completed. However, this usually means that our code gets pushed away from where it is most meaningful, so our code as a whole becomes difficult to reason with.

Promises offer a better approach. We can wrap a callback in a Promise, an object that allows us to pass the results of an operation back to where it was called from. Although the promise will initially be ‘unresolved’, we can use its then() function to eventually execute code to deal with the result when it has been resolved, crucially this can be right beside the original call.

We give a Promise object’s constructor a single function that contains our code that will eventually resolve it. Our function, when invoked, will be given functions for us to call to resolve or reject the promise, so we’re using these functions to report the outcome of the our code back to the original caller.

A function defined as async returns a Promise object in all circumstances. It can contain await operators. An await pauses execution until the promise of its own operand is resolved.

If a function returns a promise, then it can be awaited. You can even await a Promise object if it’s in a variable. An assignment with await can replace a promise.then(assign), because await pauses until the promise is resolved, at which point the assignment happens.

So, you can promise.then() and allow execution to continue, or you can await and pause execution entirely. You can even do both.

(async () => {
    let p = new Promise( (resolve,reject) => {
        resolve("I am the return value");
    });

    let r = await p.then(console.log("Resolved"));

    console.log("Done.", r);
}) ();
kevin@deb7:~/JavaScript/test$ node test.js 
 Resolved
 Done. I am the return value

Markdown notes

The following notes were extracted from http://www.flutterbys.com.au/stats/tut/tut17.3.html and summarises enough markdown to cover most code documentation requirements.

---
title: This is the title
author: D. Author
date: 14-02-2013
...

# Section 1 
# Section 2
## Section 2.1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent a velit quis ante dignissim    dignissim eget vitae tellus. Duis eget neque tellus, eu elementum leo. Nullam quis velit.

*Italic text* or _Italic text_
**bold text** or __bold text__
`courier` or ``courier``
~~strikethrough~~ 

~subscript~
^superscript^

> This is a block quotation.  Block quotations are specified by
> proceeding each line with a > character.

# Code blocks, three or more tildes
~~~~
a = rnorm(10,5,2)
for (i in 1:10) {
  print(a[1])
}
~~~~

# Section 1
1. This is the first numbered item.
2. This is the second.
1. This is the third item.  Note that the number I supplied is ignored

# Section 2
(i) This is list with roman numeral enumerators
(ii) Another item 

Horizontal rule - three or more * or - or _ even with spaces.
***
* * *
-----

# A Table. Heading alignment with dashes implies justification.
 Column A    Column B    Column C
 ---------  ----------  ---------
 Category 1    High        100.00
 Category 2    High         80.50
 ---------  ----------  ---------

This links to the [CFS search engine](http://kakapo.susa.net:8080/cfs) 

# You can even do footnotes
To create a footnote[^note1]

[^note1]: A footnote marker cannot contain any spaces.