Skip to content

Commit 9e74fa1

Browse files
committed
Commit stashed markdown files
1 parent b6e5acd commit 9e74fa1

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

content/async-javascript.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
+++
2+
title = "Async JavaScript"
3+
slug = "async-javascript"
4+
layout = "page.html"
5+
date = 2021-05-08
6+
draft = true
7+
[taxonomies]
8+
tags = ["async", "javascript"]
9+
+++
10+
11+
There are 3 ways work with **asynchronous** code in JavaScript.
12+
- Callbacks
13+
- Promises (ES6; node v4.x, Chrome 32)
14+
- Async / Await (ES7; node v8.x, Chrome 55)
15+
16+
Lets explore each of these for this example use case:
17+
```ts
18+
posts = [
19+
{ title: "Post 1", body: "This is post 1" },
20+
{ title: "Post 2", body: "This is post 2" }
21+
]
22+
```
23+
24+
Promises are immutable, the handler cannot be changed. We are also guaranteed to receive a return value: either what we intended or an error.
25+
26+
https://blog.logrocket.com/the-perfect-architecture-flow-for-your-next-node-js-project/
27+
28+
- async - pushes the operation to run asynchronously
29+
- await - pause execution until myPromise is resolved
30+
31+
## Callbacks
32+
---
33+
34+
Callbacks are unintuitive and hard to debug.
35+
36+
37+
## Promises
38+
---
39+
40+
A `Promise` is always fulfilled - either resolved or rejected.
41+
`Promise` is exclusively used in JS ecosystem: fetch API, axios, etc. So, it's a good idea to learn how to work with it properly.
42+
43+
- A `Promise` enters a 'pending' state until it is resolved or rejected.
44+
- A promise can be created using `new Promise()` constructor
45+
46+
```ts
47+
function createPost(post) {
48+
// create new promise
49+
new Promise((resolve, reject) => {
50+
setTimeout(() => {
51+
try {
52+
posts.push(post)
53+
resolve()
54+
} catch {
55+
reject("Error: Something went wrong")
56+
}
57+
})
58+
})
59+
}
60+
createPost({ title: "Post 3", body: "This is post 3" })
61+
.then(() => console.log("added"))
62+
.catch(err => console.log(err))
63+
```
64+
65+
- `Promise.all`
66+
67+
Most of the time, we'll be dealing with returned promises from these external packages.
68+
69+
```ts
70+
const promise1 = Promise.resolve('Hello world!')
71+
const promise2 = 10
72+
const promise3 = new Promise((resolve, reject) => {
73+
setTimeout(resolve, 2000, 'Goodbye')
74+
})
75+
// need to resolve 2 promises; format to JSON -> use data
76+
const promise4 = fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json())
77+
78+
// values = an array of resolved promises
79+
Promise.all([promise1, promise2, promise3, promise4]).then(values => console.log(values))
80+
```
81+
82+
## Async / Await
83+
---
84+
85+
**Await** - waits for an asynchronous process to complete.
86+
87+
NOTE: Async/Await do NOT invoke a seperate thread in the OS.
88+
<!-- Ref: <https://blog.stephencleary.com/2013/11/there-is-no-thread.html> -->
89+

content/intro-to-rust-lang.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
+++
2+
title = "Intro to Rust Lang"
3+
slug = "intro-to-rust-lang"
4+
template = "page.html"
5+
draft = true
6+
[taxonomies]
7+
tags = []
8+
+++
9+
10+
**Rust** is a memory-safe, multi-paradigm statically-typed systems programming language that features:
11+
- zero-cost abstractions
12+
- ownership semantics
13+
- trait-based generics
14+
- pattern matching (functional)
15+
- syntactic macros
16+
17+
## syntactic macros
18+
---
19+
**macros** are functions that work over syntax - macros take syntax as input and produces a new syntax as output. Instead of making function call, macros are expanded into source code. Macros enable metaprogramming in Rust.
20+
21+
Syntax: `<macro_name>!`
22+
23+
Use cases:
24+
- When you want to DRY the source code
25+
- To implement DSL
26+
- To implement *varadic interfaces*. A variadic interface takes an arbitrary number of arguments.
27+
28+
Rust macros are expanded to ASTs rather than string pre-processing like in C, C++.
29+
30+
(ref)[https://www.youtube.com/watch?v=VGk95NXaafs]
31+
32+
```rust
33+
// declaration
34+
macro_rules! x_and_y {
35+
// The arguments are prefixed by $ inside macros and type annotated by a 'designator'
36+
// Ex: 'expr' is a designator - meaning this variable $e can be any expression - virtually anything in Rust - Rust is an expression based language
37+
(x => $e:expr) => println!("X: {}", $e);
38+
(y => $e:expr) => println!("Y: {}", $e);
39+
() => {
40+
println!("This is a macro.");
41+
}
42+
}
43+
// invocation
44+
fn main() {
45+
x_and_y!();
46+
x_and_y!(x => 10);
47+
x_and_y!(y => 20 + 20);
48+
}
49+
```
50+
51+
Example: `println!("")`, `vec![]`
52+
53+
```rust
54+
fn one_even(a: i32, b: i32) -> bool {
55+
or!(even(a), even(b));
56+
}
57+
```
58+
59+
`or!` is a macro that takes a input syntax and produces a new program as output syntax
60+
61+
```rust
62+
fn one_even(a: i32, b: i32) -> bool {
63+
if even(a) {
64+
true
65+
} else {
66+
even(b)
67+
}
68+
}
69+
```
70+

content/wasm.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
+++
2+
title = "WebAssembly"
3+
slug = "webassembly"
4+
layout = "page.html"
5+
date = 2022-03-30
6+
draft = true
7+
[taxonomies]
8+
tags = ["wasm"]
9+
+++
10+
11+
# WebAssembly
12+
13+
WebAssembly or `Wasm` is binary instruction format for a stack-based VM.
14+
**Wasm** is designed as a portable target for compilation of high-level
15+
languages like C/C++/Rust, enabling deployment on the web for client and server
16+
applications.
17+
18+
- **Wasm** is basically a VM to prioritize portablity. `*.wasm` files are
19+
ingested by a runtime (like the browser) that turns this file into actual
20+
machine code of the actual machine the browser is running on and then execute
21+
that code.

0 commit comments

Comments
 (0)