JS

SYNC AND ASYNC JAVASCRIPT

SYNCHRONOUS JAVASCRIPT

01

SYNC FUNCTIONS function add (a, b) { return a + b; } function mult (a, b) { return a * b; }

A) ADD(2, 3) WILL RUN FIRST, RETURNS 5. B) MULT(5, 5) RUNS NEXT, RETURN 25 C) LASTLY, ASSIGN RES = 25 var res = multiply (add(2, 3), 5); 

ASYNC FUNCTIONS RUN INDEPENDENTLY async function add (a, b) { return a + b; }

ASYNCHRONOUS JAVASCRIPT

02

ASYNC FUNCTIONS RETURN A PROMISE. “I PROMISE TO GET BACK WITH THE RESULTS WHEN IT'S DONE”. console.log(add(99, 101)); // PROMISE

ASYNC FUNCTION async function add (a, b) { return a + b; }

RESOLVING PROMISES

03

USE “THEN” TO HANDLE THE RESULT add(99, 101) .then((res) => { console.log(res); }}

OPTIONAL - CATCH ERRORS .catch((err) => { console.log(err); })

OPTIONAL - RUN THIS REGARDLESS OF SUCCESSFUL OR ERROR .finally(() => { console.log("FINAL"); });