const keyword I spoke about before.
.freeze() prevents changes inside the object.
const prevents changing what a variable points to. I’ve demonstrated arrow functions in previous lessons, but here’s a quick recap. The first two examples here use the
function keyword, either creating a named function in the global namespace or assigning it to the
add() variable in the local context.
Why am I bringing up this tangent? Well, those labels are defined with a name and a colon. Without the parentheses in the fifth example, the curly brackets are seen as the arrow function definition and the
result: is seen as a label.
This method returns the array of the third parameter with the first and second parameter pushed inside of it. If
c isn’t given, the array returned starts out empty before
b are pushed inside. And if
b isn’t given, it is set to the value of
a + 1.
...). Here, I’ve defined a function that can take any number of arguments.
raise instead. Here, the
fruits array contains three items:
'orange'. On the second line, the array containing variables
c are assigned to the
c now contain
Support is pretty much across-the-board now but if you’re coding for older browsers, you may get into trouble. Anyhow, if you’re in sloppy mode—that’s what the cool kids term the opposite of strict—the
with keyword is like a code block based destructuring.
In the example here,
with person makes variables available in the block named for each of the attributes of
person. I wouldn’t recommend using this feature, it’s not very common anymore, but thought it should be explained in case you came across it.
08:36 A generator is a special kind of function that returns a suspended generator object. Yeah, I did it again. Another definition based on its definition. Think of a function that returns a list. That function has to create the entire list in memory, which it then returns. A generator is a way around that.
Instead of returning the whole list, it returns a promise about the list. Iterators then say to the generator, “Give me the next item in the sequence,” and the suspended generator object complies. The
yield keyword in Python is what does this magic trick of suspending the generator and returning the value.
The key part of this is the
yield indicates that this is a generator function. When the function is called, instead of doing anything, it will return an iterable generator object.
x contains a generator object. To get the next item in the sequence from the generator, I could put this in a
for loop, but all that loop actually does is use the built-in function
next(), so I’ll call that directly here. And again, and again, one more time.
The fourth iteration has the function leave the
while loop and return. When an active generator returns, it throws a
StopIteration exception signaling that it is done. If this were inside of a
for loop, the
for loop would see this exception and leave the loop.
Since I’m not in a
for loop, I see the exception. Any completion of the generator function will cause the same result. If you need to short circuit your iterator, you can use
return in your function. That’s the Python way.
There’s a local variable called
sent that is used to track the number of times the function has yielded. The
yield statement sends out the next item in the sequence and there’s a
while loop to do it
count number of times. There’s no
random() function that returns a float.
12:33 Let’s agree as friends to never mention that I did that, okay? Moving on. Notice that what comes back isn’t the random value, but an object. The object has two attributes, the value and a Boolean to indicate whether the iterator is finished.
The Python way of dealing with this feels less clunky to me. Okay, that’s better. The universe is in balance again. Calling
.next() another time, and again, and the last time. Here, you see the empty value and the
done flag set to
True, the equivalent of the
StopIteration exception in the Python world.
*) to indicate yielding from a generator. I’d be remiss if I didn’t complain about this being painful for C programmers, but I think I did that already.
'pong' is sent back from the yield, and as there was only the one
yield, the next item in the sequence is the end. The parameter that was passed back from the call to
.next() is then returned.
You resolve such a promise with the
await keyword. Why would you do this? Well, if the
greet() function here were doing something terribly complicated like waiting for a result from a server, you could take advantage of that time and do a whole bunch of computation between the promise and the
When you’re finally at a point where you absolutely must have the response back from the server, then you do the
await. If the response had come back, you’d get it immediately. If it hadn’t, you would block until it returned.
for loop inside of the
greet() function, it would get run when the function was promised, not in a separate thread.
You’d end up with some very synchronous-looking asynchronous code. To make matters worse, async functions have to be chained. If the
greet() function called another function that wasn’t asynchronous, you could get some odd results.
Array objects have an iterator method on them called
.forEach(). It isn’t built with async.
Become a Member to join the conversation.