Micro: Using Reduce for Synchronizing Async Calls
Using Reduce for Synchronizing Async Calls
So let’s say you want to slowly write out a sentence. forEach
and map
won’t wait for each async call to go through. However, if you form a reduce
properly, it has to!
In this example we will be writting text to an empty HTML element.
<div>
<pre><h1 class="my-text"></h1></pre>
</div>
const myText = document.querySelector(".my-text");
const writeChars = (myText, char) =>
new Promise((resolve, reject) => {
return setTimeout(() => {
myText.innerText += char;
resolve();
}, 25);
});
function type(sentence) {
sentence.split("").reduce((promiseResolver, char) => {
return promiseResolver.then(() => writeChars(myText, char));
}, Promise.resolve());
}
type("This is a sentence");
- First we grab the
h1
- Then we write a function that writes to
innerText
after some time - We wrap the
setTimeout
in aPromise
- We write a function called
type
- It takes in a string and splits it by char
- Each char is then iterated over via the
reduce
- We return a
Promise
ofwriteChars
and tell the reduce toresolve
saidPromise
- Each char will wait
25ms
to be typed!
Here is a little bit more advanced CodePen:
See the Pen feedread by Regis Boudinot (@selfup) on CodePen.
Conclusion
Nothing crazy, just something we all run into sometimes.
Hope you enjoyed this little micro blog!