Skip to content

Commit a43475f

Browse files
committed
minor
1 parent 48481aa commit a43475f

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
773 Bytes
Loading
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
# Promises chaining
3+
4+
There are many great things about promises. We're only starting.
5+
6+
Now we'll cover promises chaining. They allow to build sequences of asynchronous actions.
7+
8+
[cut]
9+
10+
Here's the idea:
11+
12+
```js run
13+
new Promise(function(resolve, reject) {
14+
// do a job...
15+
setTimeout(() => resolve(1), 1000);
16+
}).then(function(result) {
17+
18+
alert(result); // 1
19+
return result * 2;
20+
21+
}).then(function(result) {
22+
23+
alert(result); // 2
24+
return result * 2;
25+
26+
}).then(function(result) {
27+
28+
alert(result); // 4
29+
return result * 2;
30+
31+
});
32+
// ...
33+
```
34+
35+
As you can see:
36+
37+
- Calls to `.then` can be chained -- that's because `promise.then` returns a promise.
38+
- A value returned by `.then` becomes a result in the next `.then`.
39+
40+
If there's an error, it is also passed down the chain:
41+
42+
43+
```js run
44+
new Promise(function(resolve, reject) {
45+
setTimeout(() => resolve(1), 1000);
46+
}).then(function(result) {
47+
48+
throw new Error("Woops!");
49+
50+
}).catch(function(error) {
51+
52+
alert(error.message); // Woops!
53+
54+
});
55+
```
56+
57+
58+
59+
60+
The idea is :
61+
62+
- A callback in `.then` may return a result.
63+
64+
65+
One of main purposes of promises is to make asyn
66+
The main purpose of promises
67+
Promises
68+
69+
Promises can be chained. That allows actions to follow one after another.
70+
71+
Here's a simple example first:
72+
73+
```js
74+
let promise = new Promise(function(resolve, reject) {
75+
setTimeout(() => resolve(""))
76+
})
77+
78+
79+
80+
What if we want to
81+
The main idea behind promises
82+
Promises can be used for asynchronous tasks that eventually finish with a result or an error.
83+
84+
We already have `loadScript`

figures.sketch

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)