Check out a free preview of the full Rethinking Asynchronous JavaScript course

The "Exercise 4" Lesson is part of the full, Rethinking Asynchronous JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, you will modify the promise-based flow control code so it can be used with any number of steps.

Preview
Close

Transcript from the "Exercise 4" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Kyle: Before the break we talked about chaining promises together. But there's something fundamentally limiting about this approach. The fundamental limitation of this approach is that you have to know all of the promises in advance. You have to know how many steps are in your flow control. Sometimes, and maybe even most of the time, that is going to be the case.

[00:00:24]
You're going to know, there's a discrete set of steps that you can lay out. And you can chain your promises together to approximate whatever flow control is necessary. But there will be cases where that won't be true. There will be cases where it's impossible to predict author time, whether there might be three steps or five steps or a thousand steps.

[00:00:48]
And you probably don't wanna repeat this pattern thousand times over. That's a lot of copy and pasting, a lot of mistakes can be made, a lot of updates if you have to tweak something. So, exercise four is going to ask us to change the way our brains are thinking about it, at least a little bit.

[00:01:09]
To try to understand how we might use the functional nature of promises. The way that they work together, the way that they compose. How might we use that in such a fashion that we can get our.
>> Kyle: Get our flow control to be an arbitrary length, is what I'm trying to say.

[00:01:33]
How can we get it to an arbitrary length so that there could be one step, three steps, or 100 steps? So, we're gonna go and open up exercise four.
>> Kyle: I've already given you the get file, cuz we've already implemented that part. But what I want you to do now is I want you to think, what tools might I be able to use to create a list that could be arbitrarily long, and still have all these actions occur?

[00:02:09]
I'm trying very hard to give you hints without giving you the full solution. So if you recall, we talked about utility in a different workshop. We talked about utilities like map, which can take a list of values and transform it. And we talked about a utility called reduce, which can take a list of values and compose them together.

[00:02:34]
So if I had a list of URLs.
>> Kyle: What could I transform that list of URLs into? Maybe I could transform that list of URLs into a list of promises. And if I had a list of promises, how might I compose all those promises together? I could maybe do a reduction on the list of promises and create an arbitrarily long promise chain.

[00:03:01]
That's your exercise four. Have fun.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now