Assignment: Asynchronous Code

:one: What is asynchronous code?

Asynchronous code differs from synchronous code in that it doesn’t wait for a particular line of code to execute before it continues on to execute the next line of code.

For example, if a request was made to an external server, a synchronous function would wait for the request to be executed and returned before moving on to execute the next function.

Asynchronous code however, would send off the request, and immediately continue on to execute the next function before waiting for a response from the previous function.

:two: Describe what a callback function is

A callback function is a function that is passed into an asynchronous function which is called once the task of the parent asynchronous function is completed.

For example, in the instance of the following asynchronous function:

getWeather(function(weather) {
  console.log("The weather is: " + weather);
});

The asynchronous getWeather() function could be call synchronously, however inside of it is function(weather) which is called once we have getWeather().

So, first we get the weather, and then once we’ve got it, we call the callback function inside the parent getWeather() function, and execute the callback functions instructions…
Which in this instance is console.log("The weather is: " + weather);

:three: Why do we need asynchronous code at all? Can’t we just replace it with synchronous…

Asynchronous functions are vital for a number of reasons, most of which is arguably to do with efficiently serving requests.

If synchronous code is unable to execute, everything else must wait (sometimes indefinitely). No more executions can go ahead until the bottleneck is cleared.

This can be detrimental for servers which deal with many requests from many clients.
If there is any bottleneck or waiting period for one client, every following request thereafter must wait until the bottleneck is executed.

So with asynchronous code, we can set the wheels in motion, and then continue with other tasks. And then once the asynchronous functions or getters are returned, then we can return to the following instructions with the data that was returned to us.

It is a significantly more efficient way of processing code when you have requests to external systems which don’t require the compiler to use all resources in executing the function at hand.

:four: What are Promises?

Promises are a way in dealing with callback hell, which is a visually taxing phenomenon when dealing with multiple asynchronous functions which rely on one another to be performed.

These call functions can nest many layers deep, and when a developer attempts to modify some of the code, it can be tricky to decipher, which is more error prone.

Promises are an elegant solution to this callback hell in that instead of using functions which accept an input and a callback, a developer can make a function that returns a promise object.

A promise object is an object that represents some value that the program will obtain in the future.

:five: What does the then() function do?

A then() function is similar to the callback function in that it tells the program what to do after we get the returned information from the promise object.

So, as an example, a program will begin to fetch some data from an API call, and in the promise set by the initial function, and once we receive a response (if it resolves), we have a .then(function(nextFunction)) to continue on with the information we now have.

4 Likes

1. What is asynchronous code?
A code that doesn’t follow the sequence in which it is written rather follows the availability of resources to complete the execution.

2. Describe what a callback function is
The callback function is a function that is called right after the execution of an asynchronous call.

3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
No, we cannot always replace asynchronous with synchronized code because synchronous code holds resources that eventually leave everyone in the waiting state, giving an undesired experience to users and servers.

4. What are Promises?
Promises are one of the tools for dealing with asynchronous code. A promise is an object representing a value that is intended to exist in the future.

5. What does the then() function do?
Then() function is used to deal with asynchronous tasks. Previously callback functions were used instead of this function which made the code difficult to maintain.
This function has two parameters to deal with the success and rejection of the promise.

1 Like
  1. What is asynchronous code?
    Code that returns a value in the future when the code is finished running, which allows other functions to be run without waiting for the asynchronous functions to finish.
  2. Describe what a callback function is
    A function that is run after something else happens, done by passing a function into another function designed to take callbacks. A callback function is the function we pass as an argument into an asynchronous function.
  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous?
    Because synchronous code doesn’t run until previous code has finished running, and if any of that code is waiting for a result from outside then no code can be run until that result has arrived. This would completely freeze the system every time we need to await a response from a function. Asynchronous code allows us to “listen” for a response without stopping everything else to wait for it, and allows Javascript to multi-task its code.
  4. What are Promises?
    Functions that return a promise object, which represents a value that is intended to exist in the future.
  5. What does the then() function do?
    Runs the next function when the previous promise has been fulfilled. Allows us to queue up multiple functions to run only when previous functions have been fulfilled and not rejected. We also use .catch for rejected functions.
1 Like

What is asynchronous code?

  • Code that can schedule to run some task in the future while continuing to execute other tasks in the meantime.

Describe what a callback function is

  • Function that is called once a set task is completed

Why do we need asynchronous code at all? Can’t we just replace it with synchronous…

  • Javascript only has a single thread that would mean every synchronous call needs to be finished before executing any following code. Every user needs to wait in line for a response (from a server) if there are calls made ahead of them.

What are Promises?

  • An object representing a value that is intended to exist in the future.

What does the then() function do?

  • Function that returns a promise and takes up to two callback functions (for a success case and error handling)
1 Like
  1. What is asynchronous code?
    Asynchronous code is code that waits for input from another function to run.
  2. Describe what a callback function is
    A call back function is a function that runs when the data an asynchronous function needs is received
  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    Using synchronous code isn’t a good idea cause it can cause a webpage /server to stop running while the code waits for input.
  4. What are Promises?
    Promises are a way to handle asynchronous code. A promise object is an object that represents a value that we are going to receive in the future.
  5. What does the then() function do?
    Then() function decides what code to run after a promise object has been received.
1 Like
  1. What is asynchronous code?
    => Code where the next line does not wait for the execution of the previous line to finish, but it is not equal to concurrent or multithreaded. javascript normally is single threaded. Typically occurs when requested for external datam or with setTimeout() function.

  2. Describe what a callback function is
    => A function called from within a asynchronous function and given als parameter in that function call.

  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    => to keep code responsive: synchronous code waits till for example the requested data is received. Meanwhile nothing else is provessed for instance other tasks in a webpage or at server side respond to other requests.

  4. What are Promises?
    => A method to streamline execution: instead of calling a function with data and a callback, we call it with a value in the future.

  5. What does the then() function do?
    => Run the inner function when the outer one is complete. The next then waits (again) for its predecessor.

3 Likes

What a great article, very nicely explained.

1.) Code that doesn’t run normally top-down. A line runs before the previous line is finished
2.) A function that will be called once a task is finished
3.) Javascript is single thread, and not doing anything while waiting can make a website unresponsive → user has to wait
4.) sets up a follow up action
5.)

2 Likes

Reading Assignment: Asynchronous Code (Answers)

  1. Code that runs in an asynchronous behavior , generally single threaded.

  2. Allows asynchronous code to retrieve data.

  3. Allows for callbacks and promise to be made , unlike synchronous continuous execution.

4 Away to get ride of callback hell, a function that returns promise object.

  1. A method to specify what should happen once resolve.
2 Likes
  1. What is asynchronous code?
    Code that does not need to finish executing before it returns control to the next line;

  2. Describe what a callback function is
    A callback function is passed as a parameter to an method and is called from within that method.

  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    JavaScript is single-threaded so using synchronous code for long running operations like API calls can make a page unresponsive.

  4. What are Promises?
    Promises are objects which can be returned by asynchronous functions. They allow us to provide callback functions in a more readable manner.

  5. What does the then() function do?
    The then function allows us to specify a callback method upon the completion of an asynchronous method

1 Like
  1. What is asynchronous code?
    It allows code to be run out of order to optimize its runtime.

  2. Describe what a callback function is
    It is the function which executes when the asynchronous code is complete.

  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    We have to wait on user input and interaction to execute certain parts of our application.

  4. What are Promises?
    An object representing a value that is intended to exist in the future.

  5. What does the then() function do?
    It is used to specify what should happen after the promise is returned and resolve or reject is called.

1 Like
1

if we have 2 lines off code L1 and L2 , L1 can schedule some task to be run in the future but L2 runs before the task is completed that would be asynchronous

2

callback function is passed into another function as an parameter

3

We need it so the webpage can operate while preforming background tasks like fetching data, if we replace it with synchronous the webpage will be unresponsive waiting for every function to complete

4

Promises help us get rid of callback hell, we make a function that returns a promise object, that object is representing value that is intended to exist in the future.

5

then() function we use when we have a function that returns a promise in this function we specify what should happen once resolve or reject is called

1 Like
  1. The code that runs in parallel with another part of the code
  2. callback function is the async function which contains a callback / task which completes later
  3. So we can run another task as we wait for a long task to finish, such as network call
  4. The alternative of callback, so instead of executing the callback, we return the promise object which can wait an return in the future
  5. is a lambda that is executes after one promised function is executed and completed
2 Likes
  1. Is code that doesnt follow the javascripts internal execution flow because its resolved outside javascript.
  2. Is a function that is resolved inside of another function
  3. We use it to get away from the callback hell and write a cleaner code when using asynchronous processes.
  4. Promises are something that is supposed to happen bt we dont know when or if its even gonna resolve.
  5. Is a way of handling asynchrouns code. It makes a promise before then() happens
1 Like

1. What is asynchronous code?
It is code that isn´t processed line by line but resloved later on.
2. Describe what a callback function is.

3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous.
We need it as writing only synchronous code would mean we needed a possibility to pause the execution of all other functions which in tunr would mean our programm would not be able to do anything during this paused time.
4. What are Promises?
With promises we create a function that returns a promise object this can be a value that is going to be used in the future.
5. What does the then() function do?
It waits for an asynchronous task to finish before the next task/tasks within .then() function is executed.

2 Likes

1.What is asynchronous code?
Asynchronous code does not have to wait for other code to complete before it can start and or complete its task.

2.Describe what a callback function is
A callback function is a function that gets passed into an asynchronous function which will be called once the task completed.

3.Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
We need asynchronous code to improve the user experience.
We can’t just replace it with synchronous code because you may have to wait for a response and that you don’t know when it will return.
This behavior would jam a system running synchronous code.

4.What are Promises?
Promises help to eliminate callback hell. A promise is a function that returns a promise object.

5.What does the then() function do?
Specifies what should happen when the previous function returns a promise object.

1 Like