Skip to content

Instantly share code, notes, and snippets.

@awssimplified
Created July 30, 2020 02:08
Show Gist options
  • Save awssimplified/af89d729ae3f19bdcb7b326738820205 to your computer and use it in GitHub Desktop.
Save awssimplified/af89d729ae3f19bdcb7b326738820205 to your computer and use it in GitHub Desktop.
NodeJS
---
Every file is a 'module'
console.log(`Hi ${this.name}!`); <-- `` Templating
Make sure to use the relative path when importing or node will think you are using the built in module
exports.sayHello = function() {
console.log("Hello!");
};
^ is same as:
module.exports = {
name: "Elie",
sayHello() {
console.log("Hello!");
}
};
Default module functions:
module.exports = function() {
console.log("I am the entire module!");
};
allows you to do --> evenMoreHelpers()
Destructuring Objects
---
Allows for quick shorthand extraction of fields from objects. Example below
const obj = { first: 'Jane', last: 'Doe' };
const {first: f, last: l} = obj;
//f = 'Jane'; l = 'Doe'
// {prop} is short for {prop: prop}
const {first, last} = obj;
// first = 'Jane'; last = 'Doe'
Destructuring Arrays
---
const iterable = ['a', 'b'];
const [x, y] = iterable;
// x = 'a'; y = 'b'
Event Loop
---
Stack which holds calls to be processed immediately
Async calls get added to the event loop
Event loop is a queue
Once the stack is cleared, calls from the event loop start getting executed in order of insertion (FIFO)
async.js
---
shorthand for promise.all() <-- waiting for multiple promises to resolve before executing the callback function
Process Module
---
Used to invoke CMD arguments
environment variables accessed using process.env
process.argv gives you command line arguments
process.env.ENV_VAR
Good read for event loop stuff
---
https://blog.risingstack.com/node-hero-async-programming-in-node-js/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment