Skip to content

Commit 2900d88

Browse files
authored
Merge pull request #65 from Salah856/master
async / await
2 parents 724386a + 44aba7f commit 2900d88

File tree

7 files changed

+82
-80
lines changed

7 files changed

+82
-80
lines changed
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
The notes are below the code:
2+
الملاحظات أسفل الكود:
33

44
```js run
55
async function loadJson(url) { // (1)
@@ -17,17 +17,17 @@ loadJson('no-such-user.json')
1717
.catch(alert); // Error: 404 (4)
1818
```
1919

20-
Notes:
20+
الملاحظات:
2121

22-
1. The function `loadJson` becomes `async`.
23-
2. All `.then` inside are replaced with `await`.
24-
3. We can `return response.json()` instead of awaiting for it, like this:
22+
1. تصبح الدالة `loadJson`` غير متزامنة ".
23+
2- يتم استبدال جميع ".then" بالداخل بـ "ينتظر".
24+
3. يمكننا `` response response.json () `بدلاً من انتظارها ، كما يلي:
2525

26-
```js
27-
if (response.status == 200) {
28-
return response.json(); // (3)
29-
}
30-
```
26+
شبيبة
27+
if (response.status == 200) {
28+
رد العودة. json () ؛ // (3)
29+
}}
30+
``
3131

32-
Then the outer code would have to `await` for that promise to resolve. In our case it doesn't matter.
33-
4. The error thrown from `loadJson` is handled by `.catch`. We can't use `await loadJson(…)` there, because we're not in an `async` function.
32+
ثم يجب أن ينتظر الكود الخارجي `` ينتظر '' حتى يتم حل هذا الوعد. في حالتنا لا يهم.
33+
4. تتم معالجة الخطأ الذي تم طرحه من "loadJson" بواسطة ".catch". لا يمكننا استخدام `` انتظار انتظار Json (...) `، لأننا لسنا في وظيفة` غير متزامن '.

1-js/11-async/08-async-await/01-rewrite-async/task.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
# Rewrite using async/await
3-
4-
Rewrite this example code from the chapter <info:promise-chaining> using `async/await` instead of `.then/catch`:
2+
# أعد الكتابة باستخدام متزامن / انتظار
53

4+
أعد كتابة رمز المثال هذا من الفصل <info: prom-chaining> باستخدام `async / await` بدلاً من` .then / catch`:
65
```js run
76
function loadJson(url) {
87
return fetch(url)

1-js/11-async/08-async-await/02-rewrite-async-2/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
There are no tricks here. Just replace `.catch` with `try...catch` inside `demoGithubUser` and add `async/await` where needed:
2+
لا توجد حيل هنا. ما عليك سوى استبدال `.catch` بـ` try ... catch` داخل `demoGithubUser` وإضافة` async / await` عند الحاجة:
33

44
```js run
55
class HttpError extends Error {

1-js/11-async/08-async-await/02-rewrite-async-2/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
# Rewrite "rethrow" with async/await
2+
# أعد كتابة "rethrow" مع غير متزامن / انتظار
33

4-
Below you can find the "rethrow" example from the chapter <info:promise-chaining>. Rewrite it using `async/await` instead of `.then/catch`.
4+
يمكنك العثور أدناه على مثال "إعادة النمو" من الفصل <info: prom-chaining>. أعد كتابته باستخدام "غير متزامن / انتظار" بدلاً من ".then / catch".
55

6-
And get rid of the recursion in favour of a loop in `demoGithubUser`: with `async/await` that becomes easy to do.
6+
وتخلص من العودية لصالح حلقة في `demoGithubUser`: مع` غير متزامن / انتظار 'يصبح من السهل القيام به.
77

88
```js run
99
class HttpError extends Error {

1-js/11-async/08-async-await/03-async-from-regular/solution.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

2-
That's the case when knowing how it works inside is helpful.
2+
هذا هو الحال عند معرفة كيف يعمل في الداخل مفيد.
3+
4+
ما عليك سوى التعامل مع استدعاء `` غير متزامن '' على أنه وعد وإرفاق `` ثم '' به:
35

4-
Just treat `async` call as promise and attach `.then` to it:
56
```js run
67
async function wait() {
78
await new Promise(resolve => setTimeout(resolve, 1000));

1-js/11-async/08-async-await/03-async-from-regular/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
# Call async from non-async
2+
# استدعاء غير متزامن من غير متزامن
33

4-
We have a "regular" function. How to call `async` from it and use its result?
4+
لدينا وظيفة "عادية". كيفية استدعاء "غير متزامن" منه واستخدام نتائجه؟
55

66
```js
77
async function wait() {
@@ -17,4 +17,4 @@ function f() {
1717
}
1818
```
1919

20-
P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
20+
ملاحظة. المهمة بسيطة من الناحية الفنية ، ولكن السؤال شائع جدًا للمطورين الجدد على عدم المزامنة / الانتظار.

1-js/11-async/08-async-await/article.md

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
# Async/await
1+
# Async/await (غير المتزامن /الانتظار)
22

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+
هناك بنية خاصة للعمل مع الوعود بطريقة أكثر راحة ، تسمى "غير متزامن / انتظار". من السهل جدًا فهمها واستخدامها.
44

5-
## Async functions
5+
## الدوال غير المتزامنة
66

7-
Let's start with the `async` keyword. It can be placed before a function, like this:
7+
لنبدأ بالكلمة الرئيسية `async`. يمكن وضعها قبل دالة ، مثل هذا:
88

99
```js
1010
async function f() {
1111
return 1;
1212
}
1313
```
1414

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` ؛ دعنا نختبره:
1618

17-
For instance, this function returns a resolved promise with the result of `1`; let's test it:
1819

1920
```js run
2021
async function f() {
@@ -24,7 +25,7 @@ async function f() {
2425
f().then(alert); // 1
2526
```
2627

27-
...We could explicitly return a promise, which would be the same:
28+
... يمكن أن نعيد وعدًا صريحًا ، والذي سيكون نفسه:
2829

2930
```js run
3031
async function f() {
@@ -34,20 +35,20 @@ async function f() {
3435
f().then(alert); // 1
3536
```
3637

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 ، وتلف غير وعود فيه. بسيطة بما يكفي ، أليس كذلك؟ ولكن ليس هذا فقط. هناك كلمة رئيسية أخرى `` تنتظر '' تعمل فقط داخل وظائف `غير متزامن '، وهي رائعة جدًا.
3839

3940
## Await
4041

41-
The syntax:
42+
الصيغة:
4243

4344
```js
4445
// works only inside async functions
4546
let value = await promise;
4647
```
4748

48-
The keyword `await` makes JavaScript wait until that promise settles and returns its result.
49+
تجعل الكلمة الرئيسية "في انتظار" جافا سكريبت تنتظر حتى يستقر هذا الوعد ويعيد نتيجته.
4950

50-
Here's an example with a promise that resolves in 1 second:
51+
في ما يلي مثال بوعد يتم حله في ثانية واحدة:
5152
```js run
5253
async function f() {
5354

@@ -65,14 +66,15 @@ async function f() {
6566
f();
6667
```
6768

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+
دعونا نؤكد: `` انتظارًا '' يجعل جافا سكريبت تنتظر حتى يستقر الوعد ، ثم استمر في النتيجة. هذا لا يكلف أي موارد وحدة المعالجة المركزية ، لأن المحرك يمكنه القيام بمهام أخرى في الوقت نفسه: تنفيذ البرامج النصية الأخرى ، والتعامل مع الأحداث ، وما إلى ذلك.
6972

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+
إنها مجرد بنية أكثر أناقة للحصول على نتيجة الوعد من "الوعد. ثم" ، أسهل للقراءة والكتابة.
7174

72-
It's just a more elegant syntax of getting the promise result than `promise.then`, easier to read and write.
75+
`` `` warn header = "لا يمكن استخدام` انتظار 'في الوظائف العادية "
76+
إذا حاولنا استخدام `` انتظار '' في وظيفة غير متزامنة ، فسيكون هناك خطأ في بناء الجملة:
7377

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:
7678

7779
```js run
7880
function f() {
@@ -83,13 +85,13 @@ function f() {
8385
}
8486
```
8587

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+
`` ``
8890

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`:
9092

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. كما يجب أن نجعل الوظيفة "غير متزامنة" حتى تعمل.
9395

9496
```js run
9597
async function showAvatar() {
@@ -119,10 +121,10 @@ async function showAvatar() {
119121
showAvatar();
120122
```
121123

122-
Pretty clean and easy to read, right? Much better than before.
124+
نظيفة جدا وسهلة القراءة ، أليس كذلك؟ أفضل بكثير من ذي قبل.
123125

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+
يميل الأشخاص الذين بدأوا للتو في استخدام `` انتظار '' إلى نسيان حقيقة أنه لا يمكننا استخدام `` انتظار '' في رمز المستوى الأعلى. على سبيل المثال ، لن يعمل هذا:
126128

127129
```js run
128130
// syntax error in top-level code
@@ -142,10 +144,10 @@ But we can wrap it into an anonymous async function, like this:
142144

143145

144146
````
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`.
147+
"" `` smart header = "` `في انتظار` يقبل \" ثماني \ ""
148+
مثل "prom.then`" ، يتيح لنا "await" استخدام العناصر القابلة للاستعمال (تلك التي تستخدم طريقة `ثم` القابلة للاستدعاء). الفكرة هي أن كائن طرف ثالث قد لا يكون وعدًا ، ولكنه متوافق مع الوعد: إذا كان يدعم `.then` ، فهذا يكفي لاستخدامه مع` `بانتظار ''.
147149
148-
Here's a demo `Thenable` class; the `await` below accepts its instances:
150+
إليك فئة `` Thenable` التجريبية ؛ تقبل "الانتظار" أدناه حالاتها:
149151
150152
```js run
151153
class Thenable {
@@ -168,11 +170,11 @@ async function f() {
168170
f();
169171
```
170172
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+
`` ``
173175
174-
````smart header="Async class methods"
175-
To declare an async class method, just prepend it with `async`:
176+
"" smart header = "أساليب فئة Async"
177+
للإعلان عن أسلوب فئة غير متزامن ، ما عليك سوى إلحاقها بـ `غير متزامن ':
176178
177179
```js run
178180
class Waiter {
@@ -190,11 +192,11 @@ new Waiter()
190192
The meaning is the same: it ensures that the returned value is a promise and enables `await`.
191193
192194
````
193-
## Error handling
195+
## معالجة الأخطاء
194196

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` بشكل طبيعي ، فإن "انتظار الوعد" يُرجع النتيجة. ولكن في حالة الرفض ، فإنه يلقي الخطأ ، كما لو كان هناك بيان `رمي 'في ذلك السطر.
196198

197-
This code:
199+
هذا الكود:
198200

199201
```js
200202
async function f() {
@@ -214,9 +216,9 @@ async function f() {
214216
}
215217
```
216218

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+
في المواقف الحقيقية ، قد يستغرق الوعد بعض الوقت قبل أن يرفض. في هذه الحالة ، سيكون هناك تأخير قبل أن يطرح "انتظار" خطأ.
218220

219-
We can catch that error using `try..catch`, the same way as a regular `throw`:
221+
يمكننا اكتشاف هذا الخطأ باستخدام `try..catch` ، بالطريقة نفسها التي تستخدمها` رمية` عادية:
220222

221223
```js run
222224
async function f() {
@@ -233,7 +235,7 @@ async function f() {
233235
f();
234236
```
235237

236-
In the case of an error, the control jumps to the `catch` block. We can also wrap multiple lines:
238+
في حالة حدوث خطأ ، ينتقل عنصر التحكم إلى كتلة "الالتقاط". يمكننا أيضًا لف خطوط متعددة:
237239

238240
```js run
239241
async function f() {
@@ -250,7 +252,7 @@ async function f() {
250252
f();
251253
```
252254

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" للتعامل معها:
254256

255257
```js run
256258
async function f() {
@@ -263,17 +265,17 @@ f().catch(alert); // TypeError: failed to fetch // (*)
263265
*/!*
264266
```
265267

266-
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>.
267269

268270

269-
```smart header="`async/await` and `promise.then/catch`"
270-
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.
271+
"` `smart header =" `async / await` و` prom.then / catch` "
272+
عندما نستخدم "غير متزامن / انتظار" ، نادرًا ما نحتاج إلى ". ثم" ، لأن "انتظار" يتعامل مع الانتظار. ويمكننا استخدام 'try..catch` العادية بدلاً من `.atch'. هذا عادة (ولكن ليس دائما) أكثر ملاءمة.
271273

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+
``
274276

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" ثم "انتظر":
277279

278280
```js
279281
// wait for the array of results
@@ -284,22 +286,22 @@ let results = await Promise.all([
284286
]);
285287
```
286288

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" حول المكالمة.
288290

289-
````
291+
`` ``
290292

291-
## Summary
293+
## ملخص
292294

293-
The `async` keyword before a function has two effects:
295+
الكلمة الأساسية `غير متزامن 'قبل دالة لها تأثيران:
294296

295-
1. Makes it always return a promise.
296-
2. Allows `await` to be used in it.
297+
1. يجعلها دائما تعيد الوعد.
298+
2. يسمح باستخدام "ينتظر" لاستخدامه.
297299

298-
The `await` keyword before a promise makes JavaScript wait until that promise settles, and then:
300+
تجعل الكلمة الرئيسية "في انتظار" قبل الوعد جافا سكريبت تنتظر حتى يستقر هذا الوعد ، ثم:
299301

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. وإلا ، فإنها ترجع النتيجة.
302304

303-
Together they provide a great framework to write asynchronous code that is easy to both read and write.
305+
يوفرون معًا إطارًا رائعًا لكتابة رمز غير متزامن يسهل قراءته وكتابته.
304306

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

Comments
 (0)