DEV Community

Discussion on: Are the codes written/read left to right ?! (a misconception among beginners)

Collapse
 
jzombie profile image
jzombie

When you write asynchronous code, you can write in any direction:

For example, bottom up:

let message = ''

await Promise.all([
  new Promise(resolve => setTimeout(() => {message += 'world'; resolve()}, 200)),
  new Promise(resolve => setTimeout(() => {message += 'hello '; resolve()}, 100))
])

console.log(message) // Hello world
Enter fullscreen mode Exit fullscreen mode
Collapse
 
johnbabu021 profile image
john

Great

Collapse
 
hoomantalakian profile image
Hooman Talakian

exactly. that is what I'm talking about .as I mentioned in the article :
" Note that this code only makes sense for this example, and based on your programming language and work environment, you should make your own interpretation of this example "
It's all about "thinking" about your code and the order depends on your environment. are we in the same page?

Collapse
 
jzombie profile image
jzombie

Yep, absolutely.