Skip to main content

A Brief Guide about Docker for Developer in 2023

  What is Docker? Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is based on the idea of containers, which are a way of packaging software in a format that can be easily run on any platform. Docker provides a way to manage and deploy containerized applications, making it easier for developers to create, deploy, and run applications in a consistent and predictable way. Docker also provides tools for managing and deploying applications in a multi-container environment, allowing developers to easily scale and manage the application as it grows. What is a container? A container is a lightweight, stand-alone, and executable package that includes everything needed to run the software, including the application code, system tools, libraries, and runtime. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. It al...

JavaScript: What the heck is a Callback?

 

What is a Callback?

More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions. Functions that do this are called higher-order functions. Any function that is passed as an argument is called a callback function.

^ That’s a lot of words. Lets look at some examples to break this down a little more.

Why do we need Callbacks?

function first(){
console.log(1);
}
function second(){
console.log(2);
}
first();
second();

As you would expect, the function first is executed first, and the function second is executed second — logging the following to the console:

// 1
// 2

All good so far.

But what if function first contains some sort of code that can’t be executed immediately? For example, an API request where we have to send the request then wait for a response? To simulate this action, were going to use setTimeout which is a JavaScript function that calls a function after a set amount of time. We’ll delay our function for 500 milliseconds to simulate an API request. Our new code will look like this:

function first(){
// Simulate a code delay
setTimeout( function(){
console.log(1);
}, 500 );
}
function second(){
console.log(2);
}
first();
second();

It’s not important that you understand how setTimeout() works right now. All that matters is that you see we’ve moved our console.log(1); inside of our 500 millisecond delay. So what happens now when we invoke our functions?

first();
second();
// 2
// 1

Even though we invoked the first() function first, we logged out the result of that function after the second() function.

It’s not that JavaScript didn’t execute our functions in the order we wanted it to, it’s instead that JavaScript didn’t wait for a response from first() before moving on to execute second().

So why show you this? Because you can’t just call one function after another and hope they execute in the right order. Callbacks are a way to make sure certain code doesn’t execute until other code has already finished execution.

Create a Callback

First, open up your Chrome Developer Console (Windows: Ctrl + Shift + J)(Mac: Cmd + Option + J) and type the following function declaration into your console:

function doHomework(subject) {
alert(`Starting my ${subject} homework.`);
}

Above, we’ve created the function doHomework . Our function takes one variable, the subject that we are working on. Call your function by typing the following into your console:

doHomework('math');// Alerts: Starting my math homework.

Now lets add in our callback — as our last parameter in the doHomework() function we can pass in callback. The callback function is then defined in the second argument of our call to doHomework().

function doHomework(subject, callback) {
alert(`Starting my ${subject} homework.`);
callback();
}

doHomework('math', function() {
alert('Finished my homework');
}
);

As you’ll see, if you type the above code into your console you will get two alerts back to back: Your ‘starting homework’ alert, followed by your ‘finished homework’ alert.

But callback functions don’t always have to be defined in our function call. They can be defined elsewhere in our code like this:

function doHomework(subject, callback) {
alert(`Starting my ${subject} homework.`);
callback();
}
function alertFinished(){
alert('Finished my homework');
}
doHomework('math', alertFinished);

This result of this example is exactly the same as the previous example, but the setup is a little different. As you can see, we’ve passed the alertFinished function definition as an argument during our doHomework() function call!

A real world example

T.get('search/tweets', params, function(err, data, response) {
if(!err){
// This is where the magic will happen
} else {
console.log(err);
}
})
  • T.get simply means we are making a get request to Twitter
  • There are three parameters in this request: ‘search/tweets’, which is the route of our request, params which are our search parameters, and then an anonymous function which is our callback.

A callback is important here because we need to wait for a response from the server before we can move forward in our code. We don’t know if our API request is going to be successful or not so after sending our parameters to search/tweets via a get request, we wait. Once Twitter responds, our callback function is invoked. Twitter will either send an err (error) object or a response object back to us. In our callback function we can use an if() statement to determine if our request was successful or not, and then act upon the new data accordingly.

Comments

Popular posts from this blog

Tools and tips I wish I knew when I started Android development

  Just in case, I’m a fulltime Android developer since more than 4 years now. Step by step I’ve learned few things. It’s hard to remember everything because technologies move. But one of them is that we all need to help beginners. Beginners offer new points of view and ideas. They’re shaping our projects, our teams and (kind of) the future of platform. So I’ll try to give tips or tools, but of course first you may follow  Android guidelines . Use Jetpack libraries by default for example. JetBrains Toolbox Manage your tools with ease You may know that Android Studio is based on IntelliJ IDEA. By default you can download Android Studio install software on Android Developer website. And each time you what to update, you’ll have to download the new version and re-download the install software. To avoid this, JetBrains offer an its toolbox app which gives an easier way to track update and install them. Of course, there’s not just Android Studio but you can totally skip JetBrains ID...

Flutter — Clean Code

  Introduction: Clean code is essential in every programming language to make the code more readable, maintainable, and understandable. The same is true for Flutter. Clean code practices in Flutter are the best way to ensure that your code is easy to understand, easy to maintain, and easy to modify. In this article, we will discuss some of the best clean code practices in Flutter with examples. Follow Flutter Naming Conventions: When writing code in Flutter, it is essential to follow the naming conventions recommended by the Flutter team. Flutter follows the Dart language naming conventions. These conventions help other developers to understand your code easily. Here is an example of how to name a class in Flutter: // Good naming convention class MyClass {} // Bad naming convention class my_class {} Use Descriptive Variable and Function Names: Use descriptive variable and function names so that other developers can understand the purpose of the variable or function. Avoid using...

My First Immersion in Apple Vision Pro

  This morning, I spent half an hour trying the Apple Vision Pro headset. Here’s the punch line: This is one freaking mind-blowing piece of tech. I mean, when Steve Jobs unveiled the iPhone in 2007, you could feel the paradigm shifting in real time. This was like that, but better. In case you’ve been on news blackout for the last 24 hours, we’re talking about Apple’s augmented-reality headset. Its development was supposedly insanely expensive, internally contentious, and repeatedly delayed. But the result is so advanced and polished, it makes Meta’s VR headsets look like Blackberries. Then again, you can pick up a Meta Quest 3 for $500. Apple’s new headset won’t be available for sale until early 2024, and the price “starts at” $3,500. I know, I know—I read Twitter, too. “$3,500? Are you insane? What’s the use case? This thing is gonna belly flop like the Newton!” Also: “Nobody wants VR! Not a single VR headset so far has succeeded with the masses. It’s a dead idea!” But be car...