Skip to content

Instantly share code, notes, and snippets.

View samuraijane's full-sized avatar

Matthew Day samuraijane

View GitHub Profile
// callback functions can help us deal with asynchronous code but it can also get messy if we have multiple sequential callbacks (not shown here)
var shoppingList = ['apples', 'biscuits', 'cabbage'];
function addItem(item, callback) {
setTimeout(() => {
shoppingList.push(item);
console.log("Item added to shopping list");
callback();
}, 200);
const shoppingList = ['apples', 'biscuits', 'cabbage', 'dip'];
const isCode200 = () => Math.random() >= 0.5;
function getShoppingList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (isCode200()) {
resolve(shoppingList);
} else {
reject('There was a problem with the server, please try again.');
const shoppingList = ['apples', 'biscuits', 'cabbage', 'dip'];
const isCode200 = () => Math.random() >= 0.5;
function getData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (isCode200()) {
resolve(shoppingList);
} else {
reject('There was a problem with the server, please try again.');
@samuraijane
samuraijane / Common libraries for bootcamp students
Last active August 17, 2022 21:00
Common libraries for bootcamp students
***
BCRYPT
`npm i bcrypt`
[npm Docs](https://www.npmjs.com/package/bcrypt)
Creates a hashed string from an input. Uses callbacks, not promises.
***
BODY PARSER – DO NOT USE (SEE NOTES BELOW)
`npm i body-parser`
@samuraijane
samuraijane / Core module example
Last active August 13, 2022 20:54
Core module example
const dns = require('node:dns');
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dns.lookup('google.com', options, (err, address, family) => {
console.log('address: %j family: IPv%s', address, family)
});
// DEFINE AND EXPORT
// save this in a file named "greeting.js"
function greeting(name) {
return `Hello ${name}`;
}
module.exports = greeting;
// IMPORT AND USE
const greeting = require('./greeting.js');
@samuraijane
samuraijane / Common express methods
Last active August 20, 2022 02:50
Common express methods to handle client requests
# Server 102
## Common express methods to handle client requests
`.get`
Returns data to the client which the client has requested
URL
https://highonlife.com/members/328
REQUEST BODY
```json
null
@samuraijane
samuraijane / react-101.md
Last active September 18, 2022 18:45
React 101 – A brief introduction to React

React 101

A brief introduction to React

React

  • React is a <1> available at NPM
  • As such, any project built with React will have the file <2> even though React is used to write front-end code
  • A React project is built from separate <3> which keep the code compartmentalized
  • It was created and is maintained by developers at <4>
  • React uses a <5> to update the HTML DOM; your React code should not <6> the HTML DOM directly
  • React attaches itself to the DOM through the <7> which is located in src/index.js
@samuraijane
samuraijane / react-102.md
Last active September 19, 2022 10:57
React 102 – Managing state in React with `onClick`

React 102

Managing state in React with onClick

State

  • The term we use to refer to the value of some variable in <1> at a given moment in time is <2>
  • Understanding how and when state <3> is critical to becoming a strong developer
  • A hook is simply a <4> (before hooks, some developers would have called it a <5>)
  • All hooks in React are <6> exports so to import a React hook, <7> are necesary
  • Managing state requires a named <8> and a named <9> of your choice (the former holds a value while the latter sets the value)
  • The syntax includes a <10> on an <11>