From 76646facde80038d0c9f9612cc00add02c48d4bf Mon Sep 17 00:00:00 2001 From: JayWll Date: Sun, 23 Feb 2020 16:31:21 -0700 Subject: [PATCH] Video 95: Async/Await --- playground/9-async-await.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 playground/9-async-await.js diff --git a/playground/9-async-await.js b/playground/9-async-await.js new file mode 100644 index 0000000..de36668 --- /dev/null +++ b/playground/9-async-await.js @@ -0,0 +1,24 @@ +const add = (a, b) => { + return new Promise((resolve, reject) => { + setTimeout(() => { + if (a < 0 || b < 0) { + return reject('Numbers must be non-negative') + } + + resolve(a + b) + }, 2000) + }) +} + +const doWork = async () => { + const sum = await add(1, -99) + const sum2 = await add(sum, 50) + const sum3 = await add(sum2, -3) + return sum3 +} + +doWork().then((result) => { + console.log('result', result) +}).catch((e) => { + console.log('e', e) +})