Skip to content

Commit c5f35d1

Browse files
committed
Edited ch04.asciidoc with Atlas code editor
1 parent 18ed722 commit c5f35d1

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

ch04.asciidoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ Figuring out the tree-like nature of promises is the key to unlocking a deep und
116116
.Promisees lets you write a piece of code and visualize how the underlying graph evolves as promises are settled in fulfillment or rejection
117117
image::images/pmjs_0401.png["Promisees lets you write a piece of code and visualize how the underlying graph evolves as promises are settled in fulfillment or rejection."]
118118
119-
====
120119
****
121120

122121
A promise is created by passing the `Promise` constructor a resolver that decides how and when the promise is settled, by calling either a `resolve` method that will settle the promise in fulfillment or a `reject` method that'd settle the promise as a rejection. Until the promise is settled by calling either function, it'll be in a pending state and any reactions attached to it won't be executed. The following snippet of code creates a promise from scratch where we'll wait for a second before randomly settling the promise with a fulfillment or rejection result.
@@ -820,16 +819,16 @@ const low = [...range(random, 0, 0.8)]
820819
This sort of abstraction of complexity into another function often helps keep code focused on its intent, while striving to avoid a `for..of` loop when all we wanted was to produce a derivated sequence. It also shows how sequences can be composed and piped into one another. In this case, we first created a multipurpose and infinite `random` sequence, and then piped it through a `range` function that returns a derivated sequence that ends when it meets values that are below or above a desired range. An important aspect of iterators is that despite having been composed, the iterators produced by the `range` function can be lazily iterated as well, effectively meaning you can compose as many iterators you need into mapping, filtering, and exit condition helpers.
821820

822821
.Identifying Infinite Sequences
823-
[WARNING]
824-
====
822+
****
825823
Iterators don't have any knowledge that the sequences they produce are infinite. In a similar situation to the famous halting problem (<<fig0405>>), there is no way of knowing whether the sequence is infinite or not in code.
826824
827825
[[fig0405]]
828826
.The halting problem depicted in an https://mjavascript.com/out/xkcd-1266[XKCD comic]
829827
image::images/pmjs0405.png["The halting problem depicted in an XKCD comic: https://mjavascript.com/out/xkcd-1266."]
830828
831829
You typically have a good idea of whether a sequence is infinite or not. Whenever you have an infinite sequence it's up to you to add an escape condition that ensures the program won't crash in an attempt to loop over every single value in the sequence. While `for..of` won't run into the problem unless there's no escape condition, using mechanisms such as spread or `Array.from` would immediately result in the program crashing into an infinite loop in the case of infinite sequences.
832-
====
830+
****
831+
833832

834833
Besides the technical implications of creating iterable objects, let's go over a couple of practical examples on how we can benefit from iterators.
835834

0 commit comments

Comments
 (0)