18
loading...
This website collects cookies to deliver better user experience
setTimeout
function. We can see most functions in javascript use callbacks likeforEach
,fetch
,promise
,addEventListener
, async
await
,map
, filter
, reduce
.setTimeout
functions. Each function takes an argument. Each of the arguments are functions themselves. They are called callbacks.Each of them prints a string on console. First timer function executes after one second, second after five seconds and third after two seconds. So, that means javascript executes first then executes third console.log
because it is set to run after two seconds. And then third one after five seconds.setTimeout(function () {
console.log("Hello");
}, 1000);
setTimeout(function () {
console.log("Dear come later.");
}, 5000);
setTimeout(function () {
console.log("World");
}, 2000);
setTimeout
functions are written in code. It follows the timer which controls the order the outputs are executed. Event loop executes third setTimeout
function by loading it in call stack after it executes and removes first function from call stack.Note: The main point is second setTimeout depends on result of first function and result of third function depends on result of second setTimeout
function.
setTimeout(function () {
console.log("Hello");
setTimeout(function () {
console.log("It comes sooner.");
setTimeout(function () {
console.log("World");
}, 2000);
}, 5000);
}, 1000);
cb
as parameter of handleName function. We use callback to call string functions makeUpperCasename
and reverseName
as argument along with a string name. The handle name function takes the argument name and replace the parameter with it, and it takes a callback which is invoked inside the function. The result is uppercase fullname and reverse fullname.function makeUpperCasename(value) {
console.log(value.toUpperCase());
}
// makeUpperCasename("Johathon");
handleName("Jimmy", makeUpperCasename);
handleName("Jimmy", reverseName);
function reverseName(value) {
console.log(value.split("").reverse().join(""));
}
function handleName(name, cb) {
const fullName = `${name} Carter `;
cb(fullName);
cb(fullName);
cb(fullName);
// two gotchas, we donot invoke `cb` in function parameter
// and we can pass any functions as `cb` when we invoke it
// and any number of time.
}
const btn = document.getElementById("btn");
btn.addEventListener("click", function () {
console.log("Hello World!");
});
// console.log(btn);
resolve
and reject
which are methods of Promise API.We call these methods inside the curly braces in our logic.If we have a error in our logic we call reject
and if the code is doing well we use resolve
.const promise = new Promise((resolve, reject) => {
// resolve("I am a resolve of promise");
reject("I am a reject of promise");
});
console.log(promise);
.then
and .catch
.Both catch
and then
takes callback functions as arguments. catch
method using a parameter that we can name whatever we like, here, I use error parameter, then we print error. We print result if there is no error inside the then
method.promise
.catch((error) => {
console.error(error);
})
.then((data) => {
console.log(data);
});
async
keyword prefix. We call promise using await
afterwards. We have assigned data
as constants to store result returned.const getProducts = () => {
return new Promise((resolve, reject) => {
// get products data
resolve("fulfillling promises");
});
};
const doSomething = async () => {
const data = await getProducts();
console.log(data);
};
doSomething();
HTTP
is the API that connects our browsers to the web server. url
returns a promise.We need to handle the promise returned.async
function and await
to make sense of data returned. Other option is to handle promise with then and catch methods.const result = fetch(url)
console.log(result)
IIFE
in this example.IIFE
is immediately invoked function expression that invokes itself.fetch
call makes a request to the server and returns a response
object in body of the message. Next step is to convert that response
into human readable data
.Finally we are using .json
method on the response
object returned by the fetch network call. Then we print the data
as our result in the console.(async () => {
try {
const response = await fetch(url);
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
})();
18