You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/11-async/08-async-await/article.md
+58-56Lines changed: 58 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,21 @@
1
-
# Async/await
1
+
# Async/await (غير المتزامن /الانتظار)
2
2
3
-
There's a special syntax to work with promises in a more comfortable fashion, called "async/await". It's surprisingly easy to understand and use.
3
+
هناك بنية خاصة للعمل مع الوعود بطريقة أكثر راحة ، تسمى "غير متزامن / انتظار". من السهل جدًا فهمها واستخدامها.
4
4
5
-
## Async functions
5
+
## الدوال غير المتزامنة
6
6
7
-
Let's start with the `async` keyword. It can be placed before a function, like this:
7
+
لنبدأ بالكلمة الرئيسية `async`. يمكن وضعها قبل دالة ، مثل هذا:
8
8
9
9
```js
10
10
asyncfunctionf() {
11
11
return1;
12
12
}
13
13
```
14
14
15
-
The word "async" before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.
15
+
تعني كلمة "غير متزامن" قبل دالة شيء واحد بسيط: ترجع الدالة دائمًا الوعد. يتم تغليف القيم الأخرى في وعد تم حله تلقائيًا.
16
+
17
+
على سبيل المثال ، ترجع هذه الوظيفة وعدًا تم حله بنتيجة `1` ؛ دعنا نختبره:
16
18
17
-
For instance, this function returns a resolved promise with the result of `1`; let's test it:
18
19
19
20
```js run
20
21
asyncfunctionf() {
@@ -24,7 +25,7 @@ async function f() {
24
25
f().then(alert); // 1
25
26
```
26
27
27
-
...We could explicitly return a promise, which would be the same:
28
+
... يمكن أن نعيد وعدًا صريحًا ، والذي سيكون نفسه:
28
29
29
30
```js run
30
31
asyncfunctionf() {
@@ -34,20 +35,20 @@ async function f() {
34
35
f().then(alert); // 1
35
36
```
36
37
37
-
So, `async` ensures that the function returns a promise, and wraps non-promises in it. Simple enough, right? But not only that. There's another keyword, `await`, that works only inside `async` functions, and it's pretty cool.
38
+
لذا ، يضمن `` غير متزامن '' أن ترجع الدالة promise ، وتلف غير وعود فيه. بسيطة بما يكفي ، أليس كذلك؟ ولكن ليس هذا فقط. هناك كلمة رئيسية أخرى `` تنتظر '' تعمل فقط داخل وظائف `غير متزامن '، وهي رائعة جدًا.
38
39
39
40
## Await
40
41
41
-
The syntax:
42
+
الصيغة:
42
43
43
44
```js
44
45
// works only inside async functions
45
46
let value =await promise;
46
47
```
47
48
48
-
The keyword `await` makes JavaScript wait until that promise settles and returns its result.
49
+
تجعل الكلمة الرئيسية "في انتظار" جافا سكريبت تنتظر حتى يستقر هذا الوعد ويعيد نتيجته.
49
50
50
-
Here's an example with a promise that resolves in 1 second:
51
+
في ما يلي مثال بوعد يتم حله في ثانية واحدة:
51
52
```js run
52
53
asyncfunctionf() {
53
54
@@ -65,14 +66,15 @@ async function f() {
65
66
f();
66
67
```
67
68
68
-
The function execution "pauses" at the line `(*)` and resumes when the promise settles, with `result` becoming its result. So the code above shows "done!" in one second.
69
+
تنفيذ الوظيفة "يتوقف" عند السطر `(*)` ويستأنف عندما يستقر الوعد ، ويصبح "النتيجة" نتيجته. لذلك يظهر الرمز أعلاه "تم!" في ثانية واحدة.
70
+
71
+
دعونا نؤكد: `` انتظارًا '' يجعل جافا سكريبت تنتظر حتى يستقر الوعد ، ثم استمر في النتيجة. هذا لا يكلف أي موارد وحدة المعالجة المركزية ، لأن المحرك يمكنه القيام بمهام أخرى في الوقت نفسه: تنفيذ البرامج النصية الأخرى ، والتعامل مع الأحداث ، وما إلى ذلك.
69
72
70
-
Let's emphasize: `await` literally makes JavaScript wait until the promise settles, and then go on with the result. That doesn't cost any CPU resources, because the engine can do other jobs in the meantime: execute other scripts, handle events, etc.
73
+
إنها مجرد بنية أكثر أناقة للحصول على نتيجة الوعد من "الوعد. ثم" ، أسهل للقراءة والكتابة.
71
74
72
-
It's just a more elegant syntax of getting the promise result than `promise.then`, easier to read and write.
إذا حاولنا استخدام `` انتظار '' في وظيفة غير متزامنة ، فسيكون هناك خطأ في بناء الجملة:
73
77
74
-
````warn header="Can't use `await` in regular functions"
75
-
If we try to use `await` in non-async function, there would be a syntax error:
76
78
77
79
```js run
78
80
functionf() {
@@ -83,13 +85,13 @@ function f() {
83
85
}
84
86
```
85
87
86
-
We will get this error if we do not put `async`before a function. As said, `await` only works inside an `async function`.
87
-
````
88
+
سنحصل على هذا الخطأ إذا لم نضع `async`قبل دالة. كما ذكر ، يعمل `` انتظار '' فقط داخل `وظيفة غير متزامنة`.
89
+
````
88
90
89
-
Let's take the `showAvatar()` example from the chapter <info:promise-chaining> and rewrite it using `async/await`:
91
+
لنأخذ مثال `showAvatar()`من الفصل <info: prom-chaining> ونعيد كتابته باستخدام `async / await`:
90
92
91
-
1. We'll need to replace `.then` calls with `await`.
92
-
2. Also we should make the function `async` for them to work.
93
+
1.سنحتاج إلى استبدال مكالمات ".then" بـ "ينتظر".
94
+
2.كما يجب أن نجعل الوظيفة "غير متزامنة" حتى تعمل.
93
95
94
96
```js run
95
97
asyncfunctionshowAvatar() {
@@ -119,10 +121,10 @@ async function showAvatar() {
119
121
showAvatar();
120
122
```
121
123
122
-
Pretty clean and easy to read, right? Much better than before.
124
+
نظيفة جدا وسهلة القراءة ، أليس كذلك؟ أفضل بكثير من ذي قبل.
123
125
124
-
````smart header="`await` won't work in the top-level code"
125
-
People who are just starting to use `await` tend to forget the fact that we can't use `await` in top-level code. For example, this will not work:
126
+
````smart header = "``انتظار '' لن يعمل في رمز المستوى الأعلى"
127
+
يميل الأشخاص الذين بدأوا للتو في استخدام `` انتظار '' إلى نسيان حقيقة أنه لا يمكننا استخدام `` انتظار '' في رمز المستوى الأعلى. على سبيل المثال ، لن يعمل هذا:
126
128
127
129
```js run
128
130
// syntax error in top-level code
@@ -142,10 +144,10 @@ But we can wrap it into an anonymous async function, like this:
142
144
143
145
144
146
````
145
-
````smart header="`await` accepts \"thenables\""
146
-
Like `promise.then`, `await` allows us to use thenable objects (those with a callable `then` method). The idea is that a third-party object may not be a promise, but promise-compatible: if it supports`.then`, that's enough to use it with `await`.
مثل "prom.then`" ، يتيح لنا "await" استخدام العناصر القابلة للاستعمال (تلك التي تستخدم طريقة `ثم` القابلة للاستدعاء). الفكرة هي أن كائن طرف ثالث قد لا يكون وعدًا ، ولكنه متوافق مع الوعد: إذا كان يدعم `.then` ، فهذا يكفي لاستخدامه مع` `بانتظار ''.
147
149
148
-
Here's a demo `Thenable`class; the `await` below accepts its instances:
150
+
إليك فئة `` Thenable` التجريبية ؛ تقبل "الانتظار" أدناه حالاتها:
149
151
150
152
```js run
151
153
class Thenable {
@@ -168,11 +170,11 @@ async function f() {
168
170
f();
169
171
```
170
172
171
-
If `await`gets a non-promise object with `.then`, it calls that method providing the built-in functions `resolve` and `reject` as arguments (just as it does for a regular `Promise` executor). Then `await` waits until one of them is called (in the example above it happens in the line `(*)`) and then proceeds with the result.
172
-
````
173
+
إذا حصل `await` على كائن غير مبشر باستخدام` .then` ، فإنه يستدعي هذه الطريقة التي توفر الوظائف المضمنة `حل 'و'رفض` كوسيطات (تمامًا مثلما يفعل لمنفذ` وعد عادي'). ثم تنتظر `انتظار 'حتى يتم استدعاء أحدهم (في المثال أعلاه يحدث في السطر` (*)`) ثم يواصل النتيجة.
174
+
````
173
175
174
-
````smart header="Async class methods"
175
-
To declare an async class method, just prepend it with `async`:
176
+
"" smart header = "أساليب فئة Async"
177
+
للإعلان عن أسلوب فئة غير متزامن ، ما عليك سوى إلحاقها بـ `غير متزامن ':
176
178
177
179
```js run
178
180
class Waiter {
@@ -190,11 +192,11 @@ new Waiter()
190
192
The meaning is the same: it ensures that the returned value is a promise and enables `await`.
191
193
192
194
````
193
-
## Error handling
195
+
## معالجة الأخطاء
194
196
195
-
If a promise resolves normally, then `await promise`returns the result. But in the case of a rejection, it throws the error, just as if there were a `throw` statement at that line.
197
+
إذا تم حل الـ `promise`بشكل طبيعي ، فإن "انتظار الوعد" يُرجع النتيجة. ولكن في حالة الرفض ، فإنه يلقي الخطأ ، كما لو كان هناك بيان `رمي 'في ذلك السطر.
196
198
197
-
This code:
199
+
هذا الكود:
198
200
199
201
```js
200
202
asyncfunctionf() {
@@ -214,9 +216,9 @@ async function f() {
214
216
}
215
217
```
216
218
217
-
In real situations, the promise may take some time before it rejects. In that case there will be a delay before `await` throws an error.
219
+
في المواقف الحقيقية ، قد يستغرق الوعد بعض الوقت قبل أن يرفض. في هذه الحالة ، سيكون هناك تأخير قبل أن يطرح "انتظار" خطأ.
218
220
219
-
We can catch that error using `try..catch`, the same way as a regular `throw`:
221
+
يمكننا اكتشاف هذا الخطأ باستخدام `try..catch` ، بالطريقة نفسها التي تستخدمها` رمية` عادية:
220
222
221
223
```js run
222
224
asyncfunctionf() {
@@ -233,7 +235,7 @@ async function f() {
233
235
f();
234
236
```
235
237
236
-
In the case of an error, the control jumps to the `catch` block. We can also wrap multiple lines:
238
+
في حالة حدوث خطأ ، ينتقل عنصر التحكم إلى كتلة "الالتقاط". يمكننا أيضًا لف خطوط متعددة:
237
239
238
240
```js run
239
241
asyncfunctionf() {
@@ -250,7 +252,7 @@ async function f() {
250
252
f();
251
253
```
252
254
253
-
If we don't have `try..catch`, then the promise generated by the call of the async function `f()` becomes rejected. We can append `.catch` to handle it:
255
+
إذا لم يكن لدينا "try..catch" ، فسيتم رفض الوعد الذي تم إنشاؤه بواسطة استدعاء دالة async `f ()`. يمكننا إلحاق ".catch" للتعامل معها:
If we forget to add `.catch` there, then we get an unhandled promise error (viewable in the console). We can catch such errors using a global`unhandledrejection`event handler as described in the chapter <info:promise-error-handling>.
268
+
إذا نسينا إضافة ".catch" هناك ، فإننا نتلقى خطأ وعد غير معالج (قابل للعرض في وحدة التحكم). يمكننا اكتشاف مثل هذه الأخطاء باستخدام معالج الأحداث`unhandledrejection`العالمي كما هو موضح في الفصل <info: prom-error-handling>.
When we use `async/await`, we rarely need `.then`, because `await` handles the waiting for us. And we can use a regular `try..catch`instead of `.catch`. That's usually (but not always) more convenient.
عندما نستخدم "غير متزامن / انتظار" ، نادرًا ما نحتاج إلى ". ثم" ، لأن "انتظار" يتعامل مع الانتظار. ويمكننا استخدام 'try..catch`العادية بدلاً من `.atch'. هذا عادة (ولكن ليس دائما) أكثر ملاءمة.
271
273
272
-
But at the top level of the code, when we're outside any `async` function, we're syntactically unable to use `await`, so it's a normal practice to add `.then/catch` to handle the final result or falling-through error, like in the line `(*)`of the example above.
273
-
```
274
+
ولكن في المستوى العلوي من الشفرة ، عندما نكون خارج أي وظيفة "غير متزامن" ، يتعذر علينا استخدام "انتظار" من الناحية النحوية ، لذلك من المعتاد إضافة ".then / catch" للتعامل مع النتيجة النهائية أو خطأ السقوط ، كما هو الحال في السطر `(*)`من المثال أعلاه.
275
+
``
274
276
275
-
````smart header="`async/await` works well with `Promise.all`"
276
-
When we need to wait for multiple promises, we can wrap them in `Promise.all` and then `await`:
277
+
"``smart header =" `async / await`يعمل بشكل جيد مع" Promise.all`"
278
+
عندما نحتاج إلى انتظار وعود متعددة ، يمكننا أن نلفها في "Promise.all" ثم "انتظر":
277
279
278
280
```js
279
281
// wait for the array of results
@@ -284,22 +286,22 @@ let results = await Promise.all([
284
286
]);
285
287
```
286
288
287
-
In the case of an error, it propagates as usual, from the failed promise to `Promise.all`, and then becomes an exception that we can catch using `try..catch` around the call.
289
+
في حالة وجود خطأ ، يتم نشره كالمعتاد ، من الوعد الفاشل إلى "Promise.all" ، ثم يصبح استثناء يمكننا التقاطه باستخدام "try..catch" حول المكالمة.
288
290
289
-
````
291
+
````
290
292
291
-
## Summary
293
+
## ملخص
292
294
293
-
The `async` keyword before a function has two effects:
295
+
الكلمة الأساسية `غير متزامن 'قبل دالة لها تأثيران:
294
296
295
-
1. Makes it always return a promise.
296
-
2. Allows `await` to be used in it.
297
+
1.يجعلها دائما تعيد الوعد.
298
+
2.يسمح باستخدام "ينتظر" لاستخدامه.
297
299
298
-
The `await` keyword before a promise makes JavaScript wait until that promise settles, and then:
300
+
تجعل الكلمة الرئيسية "في انتظار" قبل الوعد جافا سكريبت تنتظر حتى يستقر هذا الوعد ، ثم:
299
301
300
-
1. If it's an error, the exception is generated — same as if `throw error` were called at that very place.
301
-
2. Otherwise, it returns the result.
302
+
1.إذا كان هناك خطأ ، فسيتم إنشاء الاستثناء - مثل استدعاء "خطأ في الخطأ" في ذلك المكان.
303
+
2.وإلا ، فإنها ترجع النتيجة.
302
304
303
-
Together they provide a great framework to write asynchronous code that is easy to both read and write.
305
+
يوفرون معًا إطارًا رائعًا لكتابة رمز غير متزامن يسهل قراءته وكتابته.
304
306
305
-
With `async/await` we rarely need to write `promise.then/catch`, but we still shouldn't forget that they are based on promises, because sometimes (e.g. in the outermost scope) we have to use these methods. Also `Promise.all` is nice when we are waiting for many tasks simultaneously.
307
+
باستخدام "غير متزامن / انتظار" ، نادرًا ما نحتاج إلى كتابة "وعد. ثم / التقاط" ، ولكن لا يزال يتعين علينا ألا ننسى أنها تستند إلى الوعود ، لأنه في بعض الأحيان (على سبيل المثال في النطاق الخارجي) علينا استخدام هذه الأساليب. كما أن "Promise.all" جميل عندما ننتظر العديد من المهام في وقت واحد.
0 commit comments