Asynchronous JS

¡Supera tus tareas y exámenes ahora con Quizwiz!

What role do web APIs play in the JS call stacking process

Web APIs process async operations, keeping the call stack clear of blocking operations. This allows for asynchronous code

Steps for working w. Promises

- Create promise using Promise() constructor - Define what should happen when the promise is fulfilled or rejected - Consume the value of a fulfilled promise, or provide a rejection reason

What is a callback function?

A f(x) that is passed to another f(x) as an argument

How JS call stack works (synchronous Tasks)

Anytime a function gets called, it gets pushed onto the call stack, and any functions called within the original function are pushed higher up onto the call stack. When a function is done executing, it gets popped off the top of the call stack, and the next function in the stack gets processed. When the call stack finishes executing the last task and there's nothing left to execute, the program finishes, leaving the call stack empty.

Asynchronous vs. Synchronous Programming

Async: Several blocks of code can run at the same time. One call doesn't stop program's execution Sync: Only one block can run at at time

JavaScript engine

Built in interpreter that executes JS code

Any time the call stack is empty, the _____ pushes the first task from the callback queue onto the call stack and runs it.

Event Loop

True or False: JS call stack can process more than one function at a time

False, it is single threaded

True or False: The call stack handles synchronous and asynchronous tasks in the same way.

False: The call stack handles asynchronous tasks in a different way than synchronous tasks. Whenever the JavaScript environment encounters code that needs to run and execute at a later time, like a setTimeout() callback or a network request, that code is handed off to a special Web API that processes the async operation. Meanwhile, the call stack moves on to other tasks then revisits the async task when it's ready to be dealt with.

True or False: When a web API is done processing an async task, the task is immediately executed on the call stack.

False: The task is added to the callback queue

call stack

JavaScript engine uses this to keep track of order of function calls and where it is in a program at any given moment

What does the event loop do?

Monitor the call stack and callback queue. Anytime the call stack is empty, the event loop takes the first task from the callback queue and pushes it onto the call stack, and runs it.

What kinds of tasks are added to the callback queue

Only callbacks for (non-blocking) operations that are scheduled to complete execution at a later time like setTimeout, setInterval and AJAX requests, for example, are added to the callback queue

Promise States

Pending, fulfilled, rejected

Promise.all()

Promise.all joins multiple individual promises into a single returned Promise when all of the promises resolve.

Higher-order functions

Takes function as an argument and/or returns a function

A promise represents:

The eventual completion of an async operation

True or False: A callback function in an asynchronous program executes at some point later, once the parent function receives the data it needs.

True

True or False: Promise.all() rejects the entire promise if any of the promises passed to it fail.

True

True or False: JavaScript can run only one task to completion before starting a new task.

True, Javascript is a synchronous, single threaded language

True or False: The asynchronous behavior of JavaScript does not come from the JavaScript engine itself

True: JavaScript engines like Chrome's V8, for instance, do not have asynchronous capabilities. Asynchronicity actually comes from the runtime environment. Runtime environments (like web browsers and Node) provide APIs that let JavaScript run tasks asynchronously.

callback queue

a list (or line) containing all the async task callbacks waiting to be executed holding area for callbacks that are waiting to be put on the call stack and eventually executed.

JavaScript engine is able to keep track of the order of function calls and where it is in a program at any given moment, using a mechanism called:

call stack


Conjuntos de estudio relacionados

Wk 1 - Practice: Ch. 3, External Analysis

View Set

CARDIO - Breath Sounds & Voice Sounds

View Set

Chapter 12: Emotional and Social Development in Adolescence

View Set

Opt Quiz 6 Micro (Market Structure)

View Set

Trade Policy and Agreements (Try it out & self-check)

View Set

Chapter 14-Nursing Management During Labor and Birth

View Set