Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 22, 2023 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/a535147d909956ef817d973bcf75c642 to your computer and use it in GitHub Desktop.
Save code-boxx/a535147d909956ef817d973bcf75c642 to your computer and use it in GitHub Desktop.
Synchronous VS Asynchronous Javascript

SYNCHRONOUS VS ASYNCHRONOUS JAVASCRIPT

https://code-boxx.com/synchronous-asynchronous-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.****

<!DOCTYPE html>
<html>
<head>
<title>Synchronous Javascript</title>
<script>
// (A) ADD FUNCTION
function add (first, second) {
return first + second;
}
// (B) MULTIPLY FUNCTION
function multiply (first, second) {
return first * second;
}
// (C) ADD, THEN MULTIPLY
var result = multiply(add(2, 3), 5);
console.log(result); // 25
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Asynchronous Javascript</title>
<script>
// (A) ASYNC ADD
async function add (first, second) {
return first + second;
}
// (B) RUN!
var added = add(2, 3);
console.log(added); // PROMISE
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Asynchronous Javascript</title>
<script>
// (A) ASYNC ADD
async function add (first, second) {
return first + second;
}
// (B) RUN ASYNC FUNCTION
add(2, 3)
// (C) THEN RESOLVE RESULT ON COMPLETE
.then(result => {
console.log(result);
// DO SOMETHING
})
// (D) OPTIONAL - CATCH ERRORS
.catch(err => console.log(error))
// (E) OPTIONAL - FINALLY
.finally(() => console.log("Finally"));
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Asynchronous Chain Javascript</title>
<script>
// (A) ADD FUNCTION
async function add (first, second) {
return first + second;
}
// (B) MULTIPLY FUNCTION
async function multiply (first, second) {
return first * second;
}
// (X) BAD!
var result = multiply(add(2, 3), 5);
console.log(result); // PROMISE - NAN
// (C) ASYNC CHAIN
add(2,3)
.then(added => {
console.log(added); // 5
multiply(added, 5)
.then(multiplied => console.log(multiplied)); // 25
});
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Asynchronous Chain Javascript</title>
<script>
// (A) ADD FUNCTION
async function add (first, second) {
return first + second;
}
// (B) MULTIPLY FUNCTION
async function multiply (first, second) {
return first * second;
}
// (C) AWAIT
async function addmultiply () {
var result = await add(2, 3);
result = await multiply(result, 5);
console.log(result); // 25
}
addmultiply();
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment