
markjreed
Modern Asynchronous JavaScript: using variable name inside its iterator? (page 7)
Within the body of the asynchronous iterator, the code uses srcArr
to refer to the array it’s being called on; since it’s a named function
and not an arrow func, why not usethis
instead? Seems like that would be less brittle and more general.
Marked As Solved

Faraz
Author of Modern Asynchronous JavaScript
Hi Mark,
Thanks for reading my book! Because we have a nested function in the iterator, this
would refer to next()
rather than srcArr
. If you replace srcArr
with this
at line 11 and 16 you’ll get an error.
One workaround is to write const self = this;
at the top level of the iterator and replace srcArr
with self
, like this:
const srcArr = [
'https://eloux.com/async_js/examples/1.json',
'https://eloux.com/async_js/examples/2.json',
'https://eloux.com/async_js/examples/3.json',
];
srcArr[Symbol.asyncIterator] = function() { //<label id="code.asyncIterator"/>
let i = 0;
const self = this;
return {
async next() {
if (i === self.length) { //<label id="code.length"/>
return {
done: true
};
}
const url = self[i++];
const response = await fetch(url);
if (!response.ok) { //<label id="code.ok"/>
throw new Error('Unable to retrieve URL: ' + url);
}
return {
value: await response.json(),
done: false
};
}
};
};
const iterator = srcArr[Symbol.asyncIterator]();
iterator.next().then(result => {
console.log(result.value.firstName); // ⇒ John
});
iterator.next().then(result => {
console.log(result.value.firstName); // ⇒ Peter
});
iterator.next().then(result => {
console.log(result.value.firstName); // ⇒ Anna
});
Let me know if you have any other question
Popular Prag Prog topics

Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New

Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list)
Typo:...
New

Hello Brian,
I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New

I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here.
...
New

This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New

The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New

I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New

I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New

Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New

Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New
Other popular topics

A thread that every forum needs!
Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New

I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New

My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New

I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

API 4
Path:
/user/following/
Method:
GET
Description:
Returns the list of all names of people whom the user follows
Response
[
{ ...
New

Saw this on TikTok of all places! :lol:
Anyone heard of them before?
Lite:
New

Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

This is a very quick guide, you just need to:
Download LM Studio: https://lmstudio.ai/
Click on search
Type DeepSeek, then select the o...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /django
- /security
- /nodejs
- /centos
- /rails
- /haskell
- /fable
- /gleam
- /js
- /swift
- /deno
- /tailwind
- /assemblyscript
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /preact
- /flutter
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /emacs
- /nim
- /vuejs
- /nerves
- /elm