diff --git a/JavaScript/1-sync-try.js b/JavaScript/1-sync-try.js index 69a12ad..0121245 100644 --- a/JavaScript/1-sync-try.js +++ b/JavaScript/1-sync-try.js @@ -7,18 +7,20 @@ const sum = (a, b) => { throw new Error('a and b should be numbers'); }; +const x = 2; +const y = 3; try { - console.log(sum(2, 3)); + const total = sum(x, y); + console.log({ x, y, total }); } catch (err) { - console.log(err.message); + console.error({ x, y, err }); } +const z = 7; +const c = 'A'; try { - console.log(sum(7, 'A')); + const res = sum(z, c); + console.log({ z, c, res }); } catch (err) { - console.log(err.message); + console.error({ z, c, err }); } - -console.log(sum(7, 'A')); - -console.log('Not executed'); diff --git a/JavaScript/2-sync-tuple.js b/JavaScript/2-sync-tuple.js index 7be00ad..84e8041 100644 --- a/JavaScript/2-sync-tuple.js +++ b/JavaScript/2-sync-tuple.js @@ -7,6 +7,18 @@ const sum = (a, b) => { return [new Error('a and b should be numbers')]; }; -console.log(sum(2, 3)); +{ + const x = 2; + const y = 3; + const [err, total] = sum(x, y); + if (err) console.error({ x, y, err, total }); + else console.log({ x, y, err, total }); +} -console.log(sum(7, 'A')); +{ + const z = 7; + const c = 'A'; + const [err, res = NaN] = sum(z, c); + if (err) console.error({ z, c, err, res }); + else console.log({ z, c, err, res }); +} diff --git a/JavaScript/3-async.js b/JavaScript/3-async.js index 9cf4c3b..f97be2c 100644 --- a/JavaScript/3-async.js +++ b/JavaScript/3-async.js @@ -8,18 +8,22 @@ const sum = (a, b, callback) => { } }; -sum(2, 3, (err, result) => { +const x = 2; +const y = 3; +sum(x, y, (err, total) => { if (err) { - console.log(err.message); + console.error({ x, y, err }); return; } - console.log(result); + console.log({ x, y, total }); }); -sum(7, 'A', (err, result) => { +const z = 7; +const c = 'A'; +sum(z, c, (err, res) => { if (err) { - console.log(err.message); + console.error({ z, c, err }); return; } - console.log(result); + console.log({ z, c, res }); }); diff --git a/JavaScript/4-uncaught.js b/JavaScript/4-uncaught.js index d3ef3c0..ece17bb 100644 --- a/JavaScript/4-uncaught.js +++ b/JavaScript/4-uncaught.js @@ -12,5 +12,16 @@ const sum = (a, b) => { throw new Error('a and b should be numbers'); }; -console.log(sum(2, 3)); -console.log(sum(7, 'A')); +{ + const x = 2; + const y = 3; + const total = sum(x, y); + console.log({ x, y, total }); +} + +{ + const z = 7; + const c = 'A'; + const res = sum(z, c); + console.log({ z, c, res }); +} diff --git a/JavaScript/5-promise.js b/JavaScript/5-promise.js index 09bed09..aed9756 100644 --- a/JavaScript/5-promise.js +++ b/JavaScript/5-promise.js @@ -1,33 +1,37 @@ 'use strict'; -const sum = (a, b) => new Promise((resolve, reject) => { +const sum = (a, b) => new Promise((totalolve, reject) => { if (typeof a === 'number' && typeof b === 'number') { - resolve(a + b); + totalolve(a + b); } else { reject(new Error('a and b should be numbers')); } }); -sum(2, 3) - .then((data) => { - console.log(data); +const x = 2; +const y = 3; +sum(x, y) + .then((total) => { + console.log({ x, y, total }); }) .catch((err) => { - console.log(err.message); + console.error({ x, y, err }); }); -sum(7, 'A') - .then((data) => { - console.log(data); +const z = 7; +const c = 'A'; +sum(z, c) + .then((res) => { + console.log({ z, c, res }); }) .catch((err) => { - console.log(err.message); + console.log({ z, c, err }); }); /* -sum(7, 'A') - .then(data => { - console.log(data); +sum(z, c) + .then((res) => { + console.log({ z, c, res }); }); UnhandledPromiseRejectionWarning: Error: a and b should be numbers diff --git a/JavaScript/6-async-await.js b/JavaScript/6-async-await.js index 3e69cf0..7b4fa4d 100644 --- a/JavaScript/6-async-await.js +++ b/JavaScript/6-async-await.js @@ -9,20 +9,27 @@ const sum = async (a, b) => { (async () => { + const x = 2; + const y = 3; try { - console.log(await sum(2, 3)); - } catch (e) { - console.log(e.message); + const total = await sum(x, y); + console.log({ x, y, total }); + } catch (err) { + console.error({ x, y, err }); } + const z = 7; + const c = 'A'; try { - console.log(await sum(7, 'A')); + const res = await sum(z, c); + console.log({ z, c, res }); } catch (err) { - console.log(err.message); + console.error({ z, c, err }); } /* - console.log(await sum(7, 'A')); + const res = await sum(z, c); + console.log({ z, c, res }); UnhandledPromiseRejectionWarning: Error: a and b should be numbers @@ -30,4 +37,5 @@ const sum = async (a, b) => { In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. */ + })(); diff --git a/JavaScript/7-async-catch.js b/JavaScript/7-async-catch.js new file mode 100644 index 0000000..2ddfed7 --- /dev/null +++ b/JavaScript/7-async-catch.js @@ -0,0 +1,28 @@ +'use strict'; + +const sum = async (a, b) => { + if (typeof a === 'number' && typeof b === 'number') { + return a + b; + } + throw new Error('a and b should be numbers'); +}; + +(async () => { + + const x = 2; + const y = 3; + const total = await sum(x, y).catch((err) => { + console.error({ x, y, err }); + return NaN; + }); + console.log({ x, y, total }); + + const z = 7; + const c = 'A'; + const res = await sum(z, c).catch((err) => { + console.error({ z, c, err }); + return NaN; + }); + console.log({ z, c, res }); + +})(); diff --git a/JavaScript/8-promise-all.js b/JavaScript/8-promise-all.js new file mode 100644 index 0000000..2b56b57 --- /dev/null +++ b/JavaScript/8-promise-all.js @@ -0,0 +1,26 @@ +'use strict'; + +const sum = async (a, b) => { + if (typeof a === 'number' && typeof b === 'number') { + return a + b; + } + throw new Error('a and b should be numbers'); +}; + +(async () => { + + const data = [ + [2, 3], + [7, 'A'], + ]; + + const [total, result] = await Promise.all( + data.map((args) => + sum(...args).catch((err) => { + console.error(err); + }) + ) + ); + console.log({ total, result }); + +})(); diff --git a/JavaScript/9-retry.js b/JavaScript/9-retry.js new file mode 100644 index 0000000..3d0b60e --- /dev/null +++ b/JavaScript/9-retry.js @@ -0,0 +1,28 @@ +'use strict'; + +const DEFAULT_RETRY = 3; + +const httpGet = async ({ url, retry = DEFAULT_RETRY }) => { + console.log({ url, retry }); + const res = await fetch(url).catch(() => ({ ok: false })); + if (!res.ok) { + const attemptsLeft = retry - 1; + if (attemptsLeft > 0) return httpGet({ url, retry: attemptsLeft }); + throw new Error('Can not get data'); + } + return res.json(); +}; + +const main = async () => { + try { + const key = '1f43ea96b1e343fe94333dd2b97a109d'; + const url = 'https://openexchangerates.org/api/latest.json?app_id=' + key; + const data = await httpGet({ url }); + const rate = data.rates['UAH']; + console.log({ rate }); + } catch (err) { + console.error({ err }); + } +}; + +main();