Skip to content

Instantly share code, notes, and snippets.

View GeekTrainer's full-sized avatar
🎯
Banging on a keyboard

Christopher Harrison GeekTrainer

🎯
Banging on a keyboard
View GitHub Profile
@GeekTrainer
GeekTrainer / demo-setup.py
Created February 6, 2024 22:17
Demo custom Django command to populate a database
import csv
import random
import getpass
from django.core.management import call_command
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from agenda.models import Talk, Track
import os
class Command(BaseCommand):
Set-ExplorerOptions -showFileExtensions
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
cinst foxitreader
cinst vscode
cinst dotnetcore-sdk
cinst python
cinst nodejs
cinst microsoft-teams
cinst slack
cinst spotify
@GeekTrainer
GeekTrainer / async-summary.md
Created June 7, 2018 15:43
Delegates, callbacks, promises, and async/await

Delegates, callbacks, promises, and async/await

I was recently asked by a junior developer what the difference between all of the above is in JavaScript. Below is my stream of consciousness.

@GeekTrainer
GeekTrainer / async-await.md
Last active February 14, 2021 14:24
Async/await

Async/await

Let's consider the following code:

function getData() {
  $.get('https://jsonplaceholder.typicode.com/posts/1')
    .then((data) => {
      return data;
 });

Promises

We closed our conversation on callbacks by highlighting one of the issues of callbacks - nesting. It's easy to find yourself nested 3-4 layers deep in even the simplest of applications. This makes code maintenance and debugging a challenge. Enter promises.

Promises build on the concept of callbacks - they both guarantee your code will be called when the operation is complete. The difference between the two is with a promise you're effectively handed an IOU. The method you're calling promises it will call your code when the operation is complete.

One important thing to note about promises is they are relatively new in JavaScript. There still exists a fair amount of legacy code that only supports callbacks. That said, you'll notice most of the libraries you'll commonly use will support promises.

Let's take another look at the get function in jQuery. Previously, we had a sample that used a callback:

@GeekTrainer
GeekTrainer / callbacks.md
Created June 4, 2018 15:42
Introducing callbacks

Introducing callbacks

Previously, I walked through the concept delegates in JavaScript. In a nutshell, a delegate is a function used as a variable, and is called by another function or operation. This is helpful when working with a function such as filter or find where you might wanto to be able to describe programatically what you're looking for.

Callbacks build on delegates, and really only differ on when they're called.

Let's consider a little bit of jQuery where we might be making a call to a remote server. The challenge here is we don't know how long the call will take, but we know we want to be able to act upon the data once the operation is complete. This is where we can use a callback. Our special delegate (called a callback) is passed into the function that will make the call to the server, with the assurance that once the operation is complete our callback's code will be executed.

$.get('http://example.com', (data) => {
@GeekTrainer
GeekTrainer / delegates.md
Last active February 14, 2021 14:24
Delegates in JavaScript

Delegates in JavaScript

One of the more confusing aspects of programming in 2018 is managing long running operations. These are typically operations that access an external system, such as a database or REST API. We don't want our code to wait if it's unnecessary, and our users certainly don't want that either. Figuring out how to let our application know it's able to do other things, while still ensuring our code is called in response to the operation, requires a little bit of coordination and work.

What I want to do with this gist, and three others, is walk through the syntax systematically, showing the evolution of Node/JavaScript/ECMAScript. Hopefully this will help solidify understanding of these key concepts.

Let's start off by taking a look at delegates, which will set the stage for the rest of the concepts.

Delegates