Skip to content

Commit 69d2459

Browse files
committed
asyn-await-how-its-called-asynchronous-when-it-makes-possible-to-execute-in-synchrounous-manner
1 parent 9c9fa79 commit 69d2459

File tree

6 files changed

+97
-29
lines changed

6 files changed

+97
-29
lines changed

Promise-Async-Await-Sequential-Execution/Promise-async-await-master-notes/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
#### A promise is an object that wraps an asynchronous operation and notifies when it’s done. This sounds exactly like callbacks, but the important differences are in the usage of Promises. Instead of providing a callback, a promise has its own methods (.then) which you call to tell the promise what will happen when it is successful or when it fails. The methods a promise provides are “then(…)” for when a successful result is available and “catch(…)” for when something went wrong.
22

3+
More simply - The Promise constructor function takes in a single argument, a (callback) function. This function is going to be passed two arguments, resolve and reject.
4+
5+
**resolve** - a function that allows you to change the status of the promise to fulfilled
6+
7+
**reject** - a function that allows you to change the status of the promise to rejected.
8+
9+
### How do you listen for when the status of a promise changes?
10+
11+
This is the most important question. It’s cool we know how to create a promise and change its status, but that’s worthless if we don’t know how to do anything after the status changes.
12+
13+
What a promise actually is. When you create a new Promise, you’re really just creating a plain old JavaScript object. This object can invoke two methods, then, and catch. Here’s the key. When the status of the promise changes to fulfilled, the function that was passed to .then will get invoked. When the status of a promise changes to rejected, the function that was passed to .catch will be invoked. What this means is that once you create a promise, you’ll pass the function you want to run if the async request is successful to .then. You’ll pass the function you want to run if the async request fails to .catch.
14+
315
In other words, **When we call a Promise function, the result from the successful path will show up in the .then(), while the error scenario will show up in the .catch()**
416

5-
#### It takes 2 arguments and both are callback functions. The first one is for the fullfilment case and the socond one is for the rejection case.
17+
#### It takes 2 arguments and both are callback functions. The first one is for the fullfilment case and the second one is for the rejection case.
618

719
#### Promise in JS means, just as the word meaning, i.e. its not the value itself, but its the promise of a value.
820

@@ -103,3 +115,7 @@ fetch(url)
103115
```
104116

105117
Assuming each of the functions, fetch(), process(), and save() return promises, process() will wait for fetch() to complete before starting, and save() will wait for process() to complete before starting. handleErrors() will only run if any of the previous promises reject.
118+
119+
#### Further Reading
120+
121+
1> [ECMAScript 2019 Language Specification Draft ECMA-262 / June 27, 2018](https://tc39.github.io/ecma262/#sec-promise-constructor)

Promise-Async-Await-Sequential-Execution/async-await-master-notes/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,13 @@ doSomethingAsync().
9292
```
9393

9494
It's important to choose which method of error handling you prefer and stick to it. Using both try/catch and .catch() at the same time will most probably lead to problems.
95+
96+
**By wrapping the logic inside an async function, we can replace the then callbacks with await statements. The effect, the code pauses execution on those lines until the Promises resolve! Asynchronous programming becomes synchronous!**
97+
98+
Async/Await enables us to write asynchronous code in a synchronous fashion, which produces cleaner and easier-to-understand logic. Under the hood, it’s just syntactic sugar using [generators and yield statements to “pause” execution](https://tc39.github.io/ecmascript-asyncawait/#async-function-definitions). In other words, async functions can “pull out” the value of a Promise even though it’s nested inside a callback function, giving us the ability to assign it to a variable!
99+
100+
##### Further Reading
101+
102+
1> [Official ES6 Drafts](https://tc39.github.io/ecmascript-asyncawait/#async-function-definitions)
103+
104+
2> [https://medium.com/siliconwat/how-javascript-async-await-works-3cab4b7d21da](https://medium.com/siliconwat/how-javascript-async-await-works-3cab4b7d21da)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
async-await uses Generators to resolve and wait for Promise.
2+
3+
await is asynchronous in async-await, when compiler reach at await it stops executing and push everything into event queue and continue with synchronous code after async function.
4+
5+
```js
6+
function first() {
7+
return new Promise(resolve => {
8+
console.log(2);
9+
resolve(3);
10+
console.log(4);
11+
});
12+
}
13+
14+
async function f() {
15+
console.log(1);
16+
let r = await first();
17+
console.log(r);
18+
}
19+
20+
console.log("a");
21+
f();
22+
console.log("b");
23+
```
24+
25+
Since await is asynchronous thus every other thing before await happens as usual
26+
27+
a
28+
1
29+
2
30+
4
31+
b
32+
// asynchronous happens
33+
3

Promise-Async-Await-Sequential-Execution/async-await-master-notes/converting-callback-to-Promise-and-async-await-1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,5 @@ Note that async functions all return Promises, so when you use return you are ju
153153
2> https://dev.to/ccleary00/how-to-rewrite-a-callback-function-in-promise-form-and-asyncawait-form-in-javascript-410e
154154

155155
3> https://medium.com/front-end-weekly/callbacks-promises-and-async-await-ad4756e01d90
156+
157+
4> https://tylermcginnis.com/async-javascript-from-callbacks-to-promises-to-async-await/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
### Transform the following code with sequential execution. I want to fire second request based on one value returned by the first request.
2+
3+
For example, the Fetch API provides an interface for making network requests, returning Promises for us to resolve. To use it to get the current weather in Long Beach, we chain together two then callbacks:
4+
5+
```js
6+
fetch("https://api.com/values/1")
7+
.then(response => response.json())
8+
.then(json => console.log(json));
9+
```
10+
11+
In the first, we create a callback function to receive the Response object and return back its JSON data. In the second, we create a callback that receives that data and then logs it to the console.
12+
13+
Need to wrap it inside an async function to make the async-await mechanism work.
14+
15+
```js
16+
const request = async () => {
17+
const response = await fetch("https://api.com/values/1");
18+
const json = await response.json();
19+
console.log(json);
20+
};
21+
22+
request();
23+
```
24+
25+
In the first line, we make a GET request to https://api.com/values/1. Instead of continuing to the next line, we wait for the request to finish, hence await. When it finishes, it passes the resolved value to the _response_ variable.
26+
27+
In the second line, we get the JSON version of the response. Again, we use await so we can wait for it to complete (or fail) and then pass the result to the json variable.
28+
29+
**By wrapping the logic inside an async function, we can replace the then callbacks with await statements. The effect, the code pauses execution on those lines until the Promises resolve! Asynchronous programming becomes synchronous!**
30+
31+
Async/Await enables us to write asynchronous code in a synchronous fashion, which produces cleaner and easier-to-understand logic. Under the hood, it’s just syntactic sugar using [generators and yield statements to “pause” execution](https://tc39.github.io/ecmascript-asyncawait/#async-function-definitions). In other words, async functions can “pull out” the value of a Promise even though it’s nested inside a callback function, giving us the ability to assign it to a variable!
32+
33+
#### Further Reading
34+
35+
https://dev.to/johnpaulada/synchronous-fetch-with-asyncawait

Promise-Async-Await-Sequential-Execution/sequential-execution-of-codes-React-Node-Context-Master-Notes/Async-await-API-call-Simple-Example-synchronous-Fetchl.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)