Skip to content

Commit 6a7df9d

Browse files
authored
Merge pull request #57 from Salah856/master
error handling with promises - promisification - microtasks
2 parents fb5a6b9 + c01f0f4 commit 6a7df9d

5 files changed

Lines changed: 96 additions & 97 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer is: **no, it won't**:
1+
الإجابة هي **no, it won't**:
22

33
```js run
44
new Promise(function(resolve, reject) {
@@ -8,6 +8,6 @@ new Promise(function(resolve, reject) {
88
}).catch(alert);
99
```
1010

11-
As said in the chapter, there's an "implicit `try..catch`" around the function code. So all synchronous errors are handled.
11+
كما ذكر في الفصل ، هناك "try...catch" ضمنية حول رمز الوظيفة. لذلك يتم التعامل مع جميع الأخطاء المتزامنة.
1212

13-
But here the error is generated not while the executor is running, but later. So the promise can't handle it.
13+
ولكن هنا يتم إنشاء الخطأ ليس أثناء تشغيل المنفذ ، ولكن في وقت لاحق. لذا الوعد لا يمكن أن ينفذ به.

1-js/11-async/04-promise-error-handling/01-error-async/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Error in setTimeout
1+
# خطأ في setTimeout
22

3-
What do you think? Will the `.catch` trigger? Explain your answer.
3+
ما رأيك؟ هل سيتم تشغيل "catch"؟ اشرح اجابتك.
44

55
```js
66
new Promise(function(resolve, reject) {

1-js/11-async/04-promise-error-handling/article.md

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

2-
# Error handling with promises
2+
# التعامل مع الأخطاء في الـ `promises`
33

4-
Promise chains are great at error handling. When a promise rejects, the control jumps to the closest rejection handler. That's very convenient in practice.
4+
سلاسل الـ `promises` رائعة في معالجة الأخطاء. عندما يرفض الوعد ، يقفز عنصر التحكم إلى أقرب معالج رفض. هذا مريح للغاية في الممارسة.
55

6-
For instance, in the code below the URL to `fetch` is wrong (no such site) and `.catch` handles the error:
6+
على سبيل المثال ، في التعليمات البرمجية أسفل عنوان URL لـ `جلب 'غير صحيح (لا يوجد مثل هذا الموقع) ويتعامل` .atch' مع الخطأ:
77

88
```js run
99
*!*
@@ -13,9 +13,9 @@ fetch('https://no-such-server.blabla') // rejects
1313
.catch(err => alert(err)) // TypeError: failed to fetch (the text may vary)
1414
```
1515

16-
As you can see, the `.catch` doesn't have to be immediate. It may appear after one or maybe several `.then`.
16+
كما ترى ، لا يجب أن يكون "الصيد" فوريًا. قد تظهر بعد واحد أو ربما عدة "ثم".
1717

18-
Or, maybe, everything is all right with the site, but the response is not valid JSON. The easiest way to catch all errors is to append `.catch` to the end of chain:
18+
أو ، ربما ، كل شيء على ما يرام مع الموقع ، ولكن الإجابة غير صالحة لـ JSON. أسهل طريقة للقبض على جميع الأخطاء هي إلحاق ".catch" بنهاية السلسلة:
1919

2020
```js run
2121
fetch('/article/promise-chaining/user.json')
@@ -38,13 +38,13 @@ fetch('/article/promise-chaining/user.json')
3838
*/!*
3939
```
4040

41-
Normally, such `.catch` doesn't trigger at all. But if any of the promises above rejects (a network problem or invalid json or whatever), then it would catch it.
41+
عادةً ، لا يتم تشغيل ".catch" على الإطلاق. ولكن إذا رفض أي من الوعود أعلاه (مشكلة في الشبكة أو json غير صالح أو أيا كان) ، فسوف يلتقطها.
4242

4343
## Implicit try..catch
4444

45-
The code of a promise executor and promise handlers has an "invisible `try..catch`" around it. If an exception happens, it gets caught and treated as a rejection.
45+
رمز تنفيذ الوعد ومعالجو الوعد يحتوي على "محاولة غير مرئية" غير مرئية حوله. إذا حدث استثناء ، يتم القبض عليه ومعاملته على أنه رفض.
4646

47-
For instance, this code:
47+
على سبيل المثال ، هذا الكود:
4848

4949
```js run
5050
new Promise((resolve, reject) => {
@@ -54,7 +54,7 @@ new Promise((resolve, reject) => {
5454
}).catch(alert); // Error: Whoops!
5555
```
5656

57-
...Works exactly the same as this:
57+
... يعمل تمامًا مثل هذا:
5858

5959
```js run
6060
new Promise((resolve, reject) => {
@@ -64,11 +64,11 @@ new Promise((resolve, reject) => {
6464
}).catch(alert); // Error: Whoops!
6565
```
6666

67-
The "invisible `try..catch`" around the executor automatically catches the error and turns it into rejected promise.
67+
الـ `try..catch` غير المرئية حول المنفذ تقوم بالتقاط الحطأ بشكل أوتوماتيكي ويحوله إلى `rejected promise`.
6868

69-
This happens not only in the executor function, but in its handlers as well. If we `throw` inside a `.then` handler, that means a rejected promise, so the control jumps to the nearest error handler.
69+
يحدث هذا ليس فقط في وظيفة المنفذ ، ولكن في معالجاتها أيضًا. إذا قمنا "بإلقاء" داخل معالج ".then" ، فهذا يعني وعدًا مرفوضًا ، لذلك ينتقل عنصر التحكم إلى أقرب معالج أخطاء.
7070

71-
Here's an example:
71+
إليك مثال:
7272

7373
```js run
7474
new Promise((resolve, reject) => {
@@ -80,7 +80,7 @@ new Promise((resolve, reject) => {
8080
}).catch(alert); // Error: Whoops!
8181
```
8282

83-
This happens for all errors, not just those caused by the `throw` statement. For example, a programming error:
83+
يحدث هذا لجميع الأخطاء ، وليس فقط تلك التي تسببها عبارة `throw`. على سبيل المثال ، خطأ في البرمجة:
8484

8585
```js run
8686
new Promise((resolve, reject) => {
@@ -92,17 +92,17 @@ new Promise((resolve, reject) => {
9292
}).catch(alert); // ReferenceError: blabla is not defined
9393
```
9494

95-
The final `.catch` not only catches explicit rejections, but also accidental errors in the handlers above.
95+
لا يكتفي "catch" النهائي برفض صريح فحسب ، بل يشتمل أيضًا على أخطاء عرضية في المعالجات أعلاه.
9696

97-
## Rethrowing
97+
## إعادة رمي الخطأ
9898

99-
As we already noticed, `.catch` at the end of the chain is similar to `try..catch`. We may have as many `.then` handlers as we want, and then use a single `.catch` at the end to handle errors in all of them.
99+
كما لاحظنا بالفعل ، ".catch" في نهاية السلسلة يشبه "try..catch". قد يكون لدينا العدد الذي تريده من معالجات "then`" ، ثم نستخدم ".catch" واحدًا في النهاية للتعامل مع الأخطاء في جميعها.
100100

101-
In a regular `try..catch` we can analyze the error and maybe rethrow it if it can't be handled. The same thing is possible for promises.
101+
في "المحاولة .. المصيد" العادية ، يمكننا تحليل الخطأ وربما إعادة ترميمه إذا لم يكن من الممكن معالجته. نفس الشيء ممكن للوعود.
102102

103-
If we `throw` inside `.catch`, then the control goes to the next closest error handler. And if we handle the error and finish normally, then it continues to the next closest successful `.then` handler.
103+
إذا قمنا `بإلقاء` داخل` .catch` ، فسينتقل عنصر التحكم إلى أقرب معالج أخطاء التالي. وإذا تعاملنا مع الخطأ وانتهينا بشكل طبيعي ، فسيستمر إلى أقرب معالج `.then` التالي.
104104

105-
In the example below the `.catch` successfully handles the error:
105+
في المثال أدناه يتعامل `catch` بنجاح مع الخطأ:
106106

107107
```js run
108108
// the execution: catch -> then
@@ -117,9 +117,9 @@ new Promise((resolve, reject) => {
117117
}).then(() => alert("Next successful handler runs"));
118118
```
119119

120-
Here the `.catch` block finishes normally. So the next successful `.then` handler is called.
120+
هنا تنتهي كتلة "الصيد" بشكل طبيعي. لذلك يتم استدعاء معالج ".then" الناجح التالي.
121121

122-
In the example below we see the other situation with `.catch`. The handler `(*)` catches the error and just can't handle it (e.g. it only knows how to handle `URIError`), so it throws it again:
122+
في المثال أدناه نرى الموقف الآخر مع ".catch". معالج `(*)` يمسك الخطأ ولا يمكنه التعامل معه (على سبيل المثال ، فهو يعرف فقط كيفية التعامل مع `URIError`) ، لذا فإنه يرميه مرة أخرى:
123123

124124
```js run
125125
// the execution: catch -> catch -> then
@@ -149,11 +149,11 @@ new Promise((resolve, reject) => {
149149
});
150150
```
151151

152-
The execution jumps from the first `.catch` `(*)` to the next one `(**)` down the chain.
152+
يقفز التنفيذ من أول ".catch" `(*)` إلى التالي `(**)` أسفل السلسلة.
153153

154-
## Unhandled rejections
154+
## حالات الرفض التي لم تتم معالجتها
155155

156-
What happens when an error is not handled? For instance, we forgot to append `.catch` to the end of the chain, like here:
156+
ماذا يحدث عندما لا يتم معالجة الخطأ؟ على سبيل المثال ، نسينا إلحاق "الصيد" بنهاية السلسلة ، كما يلي:
157157

158158
```js untrusted run refresh
159159
new Promise(function() {
@@ -164,15 +164,15 @@ new Promise(function() {
164164
}); // without .catch at the end!
165165
```
166166

167-
In case of an error, the promise becomes rejected, and the execution should jump to the closest rejection handler. But there is none. So the error gets "stuck". There's no code to handle it.
167+
Iفي حالة وجود خطأ ، يصبح الوعد مرفوضًا ، ويجب أن يقفز التنفيذ إلى أقرب معالج رفض. ولكن لا يوجد شيء. لذلك "عالق" الخطأ. لا يوجد رمز للتعامل معها.
168168

169-
In practice, just like with regular unhandled errors in code, it means that something has gone terribly wrong.
169+
من الناحية العملية ، تمامًا كما هو الحال مع الأخطاء العادية التي لم تتم معالجتها في التعليمات البرمجية ، فهذا يعني أن شيئًا ما قد حدث خطأ فادحًا.
170170

171-
What happens when a regular error occurs and is not caught by `try..catch`? The script dies with a message in the console. A similar thing happens with unhandled promise rejections.
171+
ماذا يحدث عند حدوث خطأ عادي ولا يتم اكتشافه بواسطة "try..catch"؟ يموت البرنامج النصي مع رسالة في وحدة التحكم. يحدث شيء مماثل مع رفض الوعد غير المعالج.
172172

173-
The JavaScript engine tracks such rejections and generates a global error in that case. You can see it in the console if you run the example above.
173+
يتتبع محرك جافا سكريبت حالات الرفض هذه ويولد خطأً عامًا في هذه الحالة. يمكنك رؤيته في وحدة التحكم إذا قمت بتشغيل المثال أعلاه.
174174

175-
In the browser we can catch such errors using the event `unhandledrejection`:
175+
في المتصفح يمكننا اكتشاف مثل هذه الأخطاء باستخدام الحدث `unhandledrejection`:
176176

177177
```js run
178178
*!*
@@ -188,17 +188,17 @@ new Promise(function() {
188188
}); // no catch to handle the error
189189
```
190190

191-
The event is the part of the [HTML standard](https://html.spec.whatwg.org/multipage/webappapis.html#unhandled-promise-rejections).
191+
الحدث هو جزء من [معيار HTML] (https://html.spec.whatwg.org/multipage/webappapis.html#unhandled-promise-rejections).
192192

193-
If an error occurs, and there's no `.catch`, the `unhandledrejection` handler triggers, and gets the `event` object with the information about the error, so we can do something.
193+
إذا حدث خطأ ، ولم يكن هناك "التقاط" ، فسيتم تشغيل معالج "unhandledrejection" ، ويحصل على كائن "الحدث" مع معلومات حول الخطأ ، حتى نتمكن من القيام بشيء ما.
194194

195-
Usually such errors are unrecoverable, so our best way out is to inform the user about the problem and probably report the incident to the server.
195+
عادة ما تكون مثل هذه الأخطاء غير قابلة للاسترداد ، لذلك أفضل طريقة للخروج هي إبلاغ المستخدم بالمشكلة وربما الإبلاغ عن الحادث إلى الخادم.
196196

197-
In non-browser environments like Node.js there are other ways to track unhandled errors.
197+
في البيئات غير المستعرضة مثل Node.js ، هناك طرق أخرى لتتبع الأخطاء التي لم تتم معالجتها.
198198

199-
## Summary
199+
## ملخص
200200

201-
- `.catch` handles errors in promises of all kinds: be it a `reject()` call, or an error thrown in a handler.
202-
- We should place `.catch` exactly in places where we want to handle errors and know how to handle them. The handler should analyze errors (custom error classes help) and rethrow unknown ones (maybe they are programming mistakes).
203-
- It's ok not to use `.catch` at all, if there's no way to recover from an error.
204-
- In any case we should have the `unhandledrejection` event handler (for browsers, and analogs for other environments) to track unhandled errors and inform the user (and probably our server) about them, so that our app never "just dies".
201+
- يعالج `.catch` الأخطاء في الوعود بجميع أنواعها: سواء كان استدعاء` `رفض ()` أو خطأ تم طرحه في معالج.
202+
- يجب وضع ".catch" بالضبط في الأماكن التي نرغب في معالجة الأخطاء فيها ومعرفة كيفية التعامل معها. يجب أن يقوم المعالج بتحليل الأخطاء (تساعد فئات الأخطاء المخصصة) وإعادة إنشاء الأخطاء غير المعروفة (ربما تكون أخطاء برمجة).
203+
- لا بأس بعدم استخدام "الصيد" على الإطلاق ، إذا لم تكن هناك طريقة للتعافي من خطأ.
204+
- على أي حال ، يجب أن يكون لدينا معالج الأحداث `unhandledrejection` (للمتصفحات والنظير للبيئات الأخرى) لتتبع الأخطاء التي لم تتم معالجتها وإبلاغ المستخدم (وربما خادمنا) بشأنها ، حتى لا يموت تطبيقنا أبدًا.

0 commit comments

Comments
 (0)