JS

SYNC AND ASYNC JAVASCRIPT

SYNCHRONOUS JAVASCRIPT

01

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

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

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

ASYNCHRONOUS JAVASCRIPT

02

WILL NOT WAIT FOR PROCESSING TO END, RETURNS A PROMISE. “I PROMISE TO GET BACK WITH THE RESULTS WHEN DONE”. console.log(add(99, 101)); // PROMISE

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

RESOLVING PROMISES

03

“THEN” WILL FIRE WHEN PROCESSING IS COMPLETE .then((res) => { console.log(res); }}

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

OPTIONAL - RUN THIS AFTER RESULT/ERROR .finally(() => { console.log("FINAL"); });