📜 ⬆️ ⬇️

Asynchronous JavaScript (book)

image
Answer three questions on javascript. If any of them makes it difficult for you, you should read Trevor Burnham's excellent book, Async JavaScript . A total of 90 pages, but topics such as the JavaScript event loop, synchronicity / asynchrony, single-threading / multithreading (workers), promises, fibers and much more interesting are discussed here.

So what does the following code give out?
for (var i = 1; i <= 3; i++) { setTimeout(function(){ console.log(i); }, 0); }; 

Answer
four
four
four

UPD The second question is removed - a different implementation in browsers.

How many (approximately) times does setInterval(func, 0) in a second in a browser?
 var fireCount = 0; var start = new Date; var timer = setInterval(function() { if (new Date - start > 1000) { clearInterval(timer); console.log(fireCount); return; } fireCount++; }, 0); 

Answer
Not more than 250 (most likely 240-250)

If you are wondering why this is the case, the answers are in the book .
')
Content
  • Introduction
    • Trouble in paradise
    • Who is this book for?
    • Resources for Learning JavaScript
    • Where to Turn for Help?
    • Running the Code Examples
    • Code Style in This Book
    • A Word on altJS
    • Resources for This Book

  • Understanding JavaScript Events
    • Scheduling events
    • Types of Async Functions
    • Writing Async Functions
    • Handling Async Errors
    • Un-nesting Callbacks
    • What we've learned

  • Distributing Events
    • PubSub
    • Evented models
    • Custom jQuery Events
    • What we've learned

  • Promises and Deferreds
    • A Very Brief History of Promises
    • Making promises
    • Passing Data to Callbacks
    • Progress notifications
    • Combining Promises
    • Binding to the Future with pipe
    • jQuery vs. Promises / A
    • Replacing Callbacks with Promises
    • What we've learned

  • Flow Control with Async.js
    • The Async Ordering Problem
    • Async Collection Methods
    • Organizing Tasks with Async.js
    • Dynamic Async Queuing
    • Minimalist Flow Control with Step
    • What we've learned

  • Multithreading with Workers
    • Web Workers
    • Node Workers with cluster
    • What we've learned

  • Async Script Loading
    • Limitations and Caveats
    • Reintroducing the <script> Tag
    • Programmatic Loading
    • What we've learned

  • Tools for Taming JavaScript
    • TameJS
    • Stratified js
    • Kaffeine
    • Streamline.js
    • Node-fibers
    • The Future of JavaScript: Generators

Source: https://habr.com/ru/post/207778/


All Articles