
Callbacks and Promises
Remember Java Threads?!? :D
Multithreading, daemons, asynchronous programming has always been a death note to a lot of web developers. Java web developers became Java web developers by avoiding having to deal with threads the way Java desktop (Swing, SWT etc.), Qt C++ developers do it. This is one of the most difficult aspects for a novice developer. But there’s no running away from asynchronous programming, one way or another you’ll find yourself stuck or having to solve one particular problem related to asynchronous tasks.
Alright, just like in Java there’s no way a multiple thread will execute in order or will guarantee that will happen , on the other hand, Javascript is inherently a multithreaded language itself, of course, it was first designed for use in the browser. So again, there’s no way running away from asynchronous programming.
If you’re new to callbacks and promises and is tasked to solve a lot of problems with it, my suggestion is to stay away from any ‘Promises’ frameworks out there whether it is a promise framework to handle transactions in an SQL database or ‘magically’ chain a process in Javascript. Just understand how it works and you will have full control of your code :)
Let’s take this ‘promised’ code for example:
var promise1 = new Promise(function(resolve, reject) {
resolve('Success!');
});promise1.then(function(value) {
console.log(value);
// expected output: "Success!"
});
A promise object represents an eventual completion of an asynchronous operation, it does not guarantee a resolution or rejection the way synchronous operation works. How do you call the code above in an asynchronous fashion that will eventually complete?
function myPromise() {
let promise1 = new Promise ((resolve, reject) => {
resolve('Success'); }
return promise1;}async myCall () {
const promise = await myPromise();
promise.then((value)=>{
//result 'Success'
console.log("result:", value); });}
More simple than a Java thread right? :D
Callbacks
Let’s talk about callbacks this time. If you’re familiar with using interfaces with Java threads, those are the areas that you can relate to when it comes to callbacks. However, Javascript, again has made much simpler to use by just using the callback keyword!
Let’s drill down to business. Take note callback is different from return , the latter simply return an object or value, while a callback can still run an operation synchronously.
/**
** A simple function with a callback signature*/
function withCallback (callback) {
const result = 'Success';
callback(result);
}//call here
withCallback ((result)=>{
//output 'Success'
console.log(result);
});
Callbacks are a way for Javascript to handle synchronous operations because it will always guarantee sequential execution since Javascript is asychronous by design, a lot of developers will cringe at ‘callback hell’. But don’t worry about those things too much, practice will alway make you a better coder.
Promises and Callbacks are relatable to lovelife; promises are made to be broken, callbacks are guaranteed and it is for the best interest of all to use them both. :)
