Callback, promises, and async-await || part 1

Habibul Hasan
3 min readMar 6, 2018

If you do not know how call-back function works, it will be a nightmare to understand the next few topics on this page.

Call back simply means that the called function will call another function after finishing his execution.

Rule 1: In js, you can pass a function as another function parameter. If somehow you can do that, you should be able to call this function from inside the function that has a function param in his definition. This logic is simple, as function and you !! can play with your parameter in local scope, if you assume any var==function, you should be able to play with that function also. like calling this function, or even making this function with another callback.

example:

var getUserFunction=(UserId,UserName)=>{
// you can do anything with these two var right ?console.log(UserId+UserName);}

you can simply call that like getUserFunction(“asds”,“asds”)

if you write this code like

Rule 2: As function one is expecting that it will receive two-parameter, you supplied it in the last line. But in this case, funcParam is another function[can you recall rule 1? the function can be work like var ]

Joining to rules 1+2

when we call getUserFunction with two params, inside this function [line 1 to 4] this two-argument will be passed , one as a simple var and one as a function which looks like a var . as we can play with any param that is available inside the called function , we are here making a valid function call on line number 3 !!

you can achieve the same thing as this way

a new way for your call back in the same line of calling function

Realization: Callback function promise you that I will execute another function after this itself finish its execution.

Bullshit !! Do I need to use this callback just to show a message? wait for a minute, don’t you know that your JS code is async? Let me tell you a story.

I wrote my code like this way after I fell the same!

async call will not wait until user loaded inside setTimeout

Async call will not wait for loading user inside Time out function. that's why on line 13 you will be kicked!

So, this case tells you to do something so that you get your user from database and validate it in some way ! for the validation you need to use call back, cause this will keep your promise, only call back can wait until this function gets executed, no one else ! Love your call back !

love your call back

whenever you see a place where you are passing a function as a parameter, you can be sure that called function will invoke that function.

See more on part two about promise

--

--

Habibul Hasan

Detailed oriented human being! love to code , ride and travel