From d8169280aa6fe1bc97c0c16f3ebc106aa355ad3c Mon Sep 17 00:00:00 2001 From: oleksii golikov Date: Mon, 9 Jul 2018 21:49:11 +0300 Subject: [PATCH] for await example --- JavaScript/a-for-await.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 JavaScript/a-for-await.js diff --git a/JavaScript/a-for-await.js b/JavaScript/a-for-await.js new file mode 100644 index 0000000..c445378 --- /dev/null +++ b/JavaScript/a-for-await.js @@ -0,0 +1,38 @@ +'use strict'; + +async function * slowCounter() { + let counter = 0; + while (true) { + counter += 1; + yield new Promise((resolve) => { + setTimeout(() => resolve(counter), 1000); + }) + } +} + +async function * fastCounterDown() { + let counter = 1000000; + while (true) { + counter -= 1; + yield counter; + } +} + +async function* zip (a, b) { + while (true) { + const [first, second] = await Promise.all([ + a.next(), + b.next(), + ]); + + yield [first.value, second.value]; + } +} + +async function main () { + for await (let it of zip(slowCounter(), fastCounterDown())) { + console.log(it); + } +} + +main(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy