JS Event Loop, Promises, Async Await etc. Slava Kim

Similar documents
NODE.JS MOCK TEST NODE.JS MOCK TEST I

Introduction to Coroutines. Roman Elizarov elizarov at JetBrains

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

Asynchronous JS. SWE 432, Fall Web Application Development

Swift 5, ABI Stability and

Backend Development. SWE 432, Fall Web Application Development

Fresh Async With Kotlin. Presented at QCon SF, 2017 /Roman JetBrains

INF5750. Introduction to JavaScript and Node.js

JavaScript. The Bad Parts. Patrick Behr

Asynchronous I/O: A Case Study in Python

Composable Concurrency in Perl 6. Jonathan Worthington

Module 6 Node.js and Socket.IO

NODE.JS SERVER SIDE JAVASCRIPT. Introduc)on Node.js

Composable Concurrency in Perl 6. Jonathan Worthington

Manoj Kumar- From Call back's hell to using Async Await: Automated testing with JavaScript

Design and Implementation of Modern Programming Languages (Seminar)

Introduction to Concurrency. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 4 01/21/2010

WebRTC: Possible? Don McGregor Research Associate MOVES Institute.

Comet and WebSocket Web Applications How to Scale Server-Side Event-Driven Scenarios

Asynchronous Programming Demystified

ECMAScript 2015 and beyond The Future of JavaScript is Now!

Web Application Development

Full Stack boot camp

Actors in the Small. Making Actors more Useful. Bill La

Asynchronous Programming in Javascript, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 19 10/25/2016

Welcome to CS50 section! This is Week 10 :(

Reactive Web Programming

Concurrency Analysis of Asynchronous APIs

CS193X: Web Programming Fundamentals

Internet Programming Week 10. Instructor: Paulo Fernandes Applied Computer Science University of Winnipeg

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Chris Buckett. FOREWORD BY Seth Ladd MANNING 6$03/(&+$37(5

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

Inside core.async Channels. Rich Hickey

HTML5 Evolution and Development. Matt Spencer UI & Browser Marketing Manager

Parallelism, Concurrency, and Asynchrony in Perl 6. Jonathan Worthington

Ten interesting features of Google s Angular Project

Web API Lab. The next two deliverables you shall write yourself.

A Sense of Time for JavaScript and Node.js

CSCI 201L Written Exam #1 Fall % of course grade

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

High performance reactive applications with Vert.x

Why I still develop synchronous web in the asyncio era. April 7th, 2017 Giovanni Barillari - pycon otto - Firenze, Italy

Chapter 4: Threads. Operating System Concepts 9 th Edition

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Chapter 1: Distributed Information Systems

Reactive Java: Promises and Streams with Reakt. Geoff Chandler and Rick Hightower

CSE 153 Design of Operating Systems

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Don't Call Us, We'll Call You:

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Computation at the Speed of Monads July Computation at the Speed of Monads. Brigham Young University - Idaho.

AJAX: Asynchronous Event Handling Sunnie Chung

Server-Side JavaScript auf der JVM. Peter Doschkinow Senior Java Architect

News in RSA-RTE 10.2 updated for sprint Mattias Mohlin, January 2018

Performance Case Study

Virtual machines (e.g., VMware)

ASYNCHRONOUS PROGRAMMING IN C# 5 WITHOUT USE OF MULTIPLE THREADS

Chapter 4: Threads. Operating System Concepts 9 th Edition

jquery and AJAX

Consistency: Relaxed. SWE 622, Spring 2017 Distributed Software Engineering

The Various Faces of the.net Task Parallel Library

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

CMSC 132: Object-Oriented Programming II

Race Conditions & Synchronization

Concurrent Programming in C++ Venkat

MULTI-THREADED QUERIES

7/6/2015. Motivation & examples Threads, shared memory, & synchronization. Imperative programs

Introducing Shared-Memory Concurrency

CIS 3308 Web Application Programming Syllabus

20480C: Programming in HTML5 with JavaScript and CSS3. Course Code: 20480C; Duration: 5 days; Instructor-led. JavaScript code.

Introduction to Asynchronous Programming Fall 2014

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Cross-compiling C++ to JavaScript. Challenges in porting the join.me common library to HTML5

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

SOFTWARE MAINTENANCE AND EVOLUTION --- REFACTORING FOR ASYNC --- CS563 WEEK 3 - THU

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Motivation & examples Threads, shared memory, & synchronization

Server execution of JavaScript: What could possibly go wrong?

55249: Developing with the SharePoint Framework Duration: 05 days

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

DNParallel - Version: 3. TPL Dataflow

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Foundations of the C++ Concurrency Memory Model

10/18/2017. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

welcome to BOILERCAMP HOW TO WEB DEV

Programming in the Life Sciences

Threads Questions Important Questions

Tools. SWE 432, Fall Design and Implementation of Software for the Web

Simplifying Asynchronous Code with

Mobile App:IT. Methods & Classes

Asynchronous Programming - Done right

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

University of Karlsruhe (TH)

CS 3 Introduction to Software Engineering. 3: Exceptions

Chapter 4: Multithreaded Programming

Spectre, Meltdown, and the Impact of Security Vulnerabilities on your IT Environment. Orin Jeff Melnick

Transcription:

JS Event Loop, Promises, Async Await etc Slava Kim

Synchronous Happens consecutively, one after another

Asynchronous Happens later at some point in time

Parallelism vs Concurrency

What are those???? Concurrency - multiple tasks are handled Parallelism - doing multiple tasks at the same time

Parallelism vs Concurrency TA

Parallelism vs Concurrency TA Student

Parallelism vs Concurrency TA

Parallelism vs Concurrency TA

Parallelism vs Concurrency TA

Parallelism vs Concurrency TA

Parallelism vs Concurrency TA

Is TA concurrent or parallel? TA is not parallel - there is only one TA TA is concurrent tho, the TA goes around and is helping 3 students simultaneously, but some students need to wait

Parallelism vs Concurrency TA TA

Parallelism vs Concurrency TA TA

Parallelism vs Concurrency TA TA

Parallelism vs Concurrency TA TA

Parallelism vs Concurrency TA TA

Is TA concurrent or parallel? There are multiple TAs - we can achieve parallelism with concurrency Still handling multiple students at the same time

CPU analogy Each TA - one CPU Each student - a separate task Some programming languages allow you to use multiple cores (C++, Java) JavaScript is single threaded, can only use one core, but is still concurrent How?

Queue TA

Queue TA

Queue TA

Queue JS click keypressclick click time out

Why callbacks?

Why callbacks? click

Callbacks to handle a result of an operation result

Time outs and intervals timeout

Event Loop JS click result click timeout Each individual event is handled in some order New events are queued up in the end

Somewhere in C++ implementation of JS Engine

Event Loops example

Event Loops example A C (2 second delay) B

Event Loops example

Event Loops example A C (virtually no delay) B

Event Loops: Good It is a simple model to work with to achieve concurrency No need for locks, or critical sections. Each function is a critical section Good when you have a lot of I/O work (most web servers, UI systems) Examples of I/O: talking to database, handling requests, waiting for user to click

Event Loop: bad CPU intensive operations will bring down your browser/server Can be confusing for new users No real way to predict in which order events will happen

Promises - motivation

Promises - motivation Callback hell!

Possible solution with promises Only one additional level of nesting Error handling can be grouped together Only one branch of continuation

Possible solution with promises Only one additional level of nesting Error handling can be grouped together Only one branch of continuation

Promises - await all

Promises - await all

Promises - race

Promises - availability Available in all modern browsers (rip IE) Available in latest Node.js Works with MongoDB client Might need to wrap other libraries into promises interface with promisify Makes code cleaner and easier to manage, yay! Your interviewers will impressed

Async/Await New feature in the latest spec of JavaScript Can await on anything that returns a promise Is not yet available widely without special setup Makes code even nicer

Callback hell!

Possible solution with promises

Solution with Async/Await No extra indentation or nesting at all Code reads synchronous but actually is async Handle errors with try/catch construct just like synchronous code

Solution with Async/Await No extra indentation or nesting at all Code reads synchronous but actually is async Handle errors with try/catch construct just like synchronous code

Why don t we use it now? To make it work, you need to setup a compiler from newest JS to old JS Every function that makes use of await needs to be marked async Can make code difficult to follow

But you still can use it Supported in latest node, latest Chrome and latest Firefox Probably will break on a lot of other places [demo]