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/05-promise-api/article.md
+64-62Lines changed: 64 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
-
# Promise API
1
+
# Promise API
2
2
3
-
There are 5 static methods in the `Promise` class. We'll quickly cover their use cases here.
3
+
هناك خمس دوال ثابتة في `Promise` class. سنغطي بسرعة حالات استخدامهم هنا.
4
4
5
5
## Promise.all
6
6
7
-
Let's say we want many promises to execute in parallel and wait until all of them are ready.
7
+
لنفترض أننا نريد تنفيذ العديد من الوعود بالتوازي والانتظار حتى تصبح جميعها جاهزة.
8
8
9
-
For instance, download several URLs in parallel and process the content once they are all done.
9
+
على سبيل المثال ، قم بتنزيل العديد من عناوين URL بالتوازي وقم بمعالجة المحتوى بمجرد الانتهاء منها.
10
10
11
-
That's what `Promise.all` is for.
11
+
هذا هو الغرض من "Promise.all".
12
12
13
-
The syntax is:
13
+
يكتب على النحو التالي:
14
14
15
15
```js
16
16
let promise =Promise.all([...promises...]);
17
17
```
18
18
19
-
`Promise.all`takes an array of promises (it technically can be any iterable, but is usually an array) and returns a new promise.
19
+
يأخذ `Promise.all`مجموعة من الوعود (من الناحية التقنية يمكن أن تكون قابلة للتكرار ، ولكن عادة ما تكون صفيفًا) ويعيد وعدًا جديدًا.
20
20
21
-
The new promise resolves when all listed promises are settled, and the array of their results becomes its result.
21
+
يتم حل الوعد الجديد عندما تتم تسوية جميع الوعود المدرجة ، وتصبح مجموعة نتائجها نتاجها.
22
22
23
-
For instance, the `Promise.all` below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
23
+
على سبيل المثال ، يستقر "Promise.all" أدناه بعد 3 ثوانٍ ، ومن ثم تكون نتائجه مصفوفة `[1 ، 2 ، 3]`:
24
24
25
25
```js run
26
26
Promise.all([
@@ -30,11 +30,11 @@ Promise.all([
30
30
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
31
31
```
32
32
33
-
Please note that the order of the resulting array members is the same as in its source promises. Even though the first promise takes the longest time to resolve, it's still first in the array of results.
33
+
يرجى ملاحظة أن ترتيب أعضاء الصفيف الناتج هو نفسه كما في وعود المصدر. على الرغم من أن الوعد الأول يستغرق وقتًا أطول للحل ، إلا أنه لا يزال الأول في مجموعة النتائج.
34
34
35
-
A common trick is to map an array of job data into an array of promises, and then wrap that into`Promise.all`.
35
+
الحيلة الشائعة هي تعيين مصفوفة من بيانات العمل إلى مجموعة من promises ، ثم لفها في`Promise.all`.
36
36
37
-
For instance, if we have an array of URLs, we can fetch them all like this:
37
+
على سبيل المثال ، إذا كان لدينا مجموعة من عناوين URL ، فيمكننا جلبها جميعًا مثل هذا:
38
38
39
39
```js run
40
40
let urls = [
@@ -53,7 +53,7 @@ Promise.all(requests)
53
53
));
54
54
```
55
55
56
-
A bigger example with fetching user information for an array of GitHub users by their names (we could fetch an array of goods by their ids, the logic is identical):
56
+
مثال أكبر على جلب معلومات المستخدم لمجموعة من مستخدمي GitHub بأسمائهم (يمكننا جلب مجموعة من السلع حسب معرفاتهم ، المنطق متطابق):
**If any of the promises is rejected, the promise returned by `Promise.all` immediately rejects with that error.**
78
+
** إذا تم رفض أي من الوعود ، فإن الوعد الذي تم إرجاعه بواسطة "Promise.all" يرفض على الفور هذا الخطأ. **
79
79
80
-
For instance:
80
+
على سبيل المثال:
81
81
82
82
```js run
83
83
Promise.all([
@@ -89,20 +89,20 @@ Promise.all([
89
89
]).catch(alert); // Error: Whoops!
90
90
```
91
91
92
-
Here the second promise rejects in two seconds. That leads to an immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the entire `Promise.all`.
92
+
هنا الوعد الثاني يرفض في ثانيتين. يؤدي ذلك إلى الرفض الفوري لـ "Promise.all" ، وبالتالي يتم تنفيذ ".catch": يصبح خطأ الرفض نتيجة لـ "Promise.all" بالكامل.
93
93
94
-
```warn header="In case of an error, other promises are ignored"
95
-
If one promise rejects, `Promise.all` immediately rejects, completely forgetting about the other ones in the list. Their results are ignored.
94
+
```warn header="في حالة حدوث خطأ ، يتم تجاهل الوعود الأخرى"
95
+
إذا رفض أحد الوعود ، رفض `Promise.all` على الفور ، متناسين تمامًا الوعود الأخرى في القائمة. يتم تجاهل نتائجهم.
96
96
97
-
For example, if there are multiple `fetch` calls, like in the example above, and one fails, the others will still continue to execute, but `Promise.all` won't watch them anymore. They will probably settle, but their results will be ignored.
97
+
على سبيل المثال ، إذا كان هناك العديد من مكالمات `الجلب` ، كما هو موضح في المثال أعلاه ، وفشل أحدها ، فسيستمر الآخرون في التنفيذ ، ولكن لن يُشاهدهم" الوعد. جميع "بعد الآن. ربما سيستقرون ، ولكن سيتم تجاهل نتائجهم.
98
98
99
-
`Promise.all` does nothing to cancel them, as there's no concept of "cancellation" in promises. In [another chapter](info:fetch-abort) we'll cover `AbortController` that can help with that, but it's not a part of the Promise API.
99
+
لا يقوم "Promise.all" بأي شيء لإلغائها ، حيث لا يوجد مفهوم "الإلغاء" في الوعود. في [فصل آخر] (info:fetch-abort) سنغطي `AbortController` التي يمكن أن تساعد في ذلك ، لكنها ليست جزءًا من Promise API.
100
100
```
101
101
102
-
````smart header="`Promise.all(iterable)`allows non-promise \"regular\" values in `iterable`"
103
-
Normally, `Promise.all(...)`accepts an iterable (in most cases an array) of promises. But if any of those objects is not a promise, it's passed to the resulting array "as is".
102
+
````smart header="`Promise.all(iterable)`يسمح بقيم ليست promise في "قابل للتكرار" "
103
+
عادة ، يقبل `Promise.all(...)`الوعود (في معظم الحالات مجموعة) من الوعود. ولكن إذا لم يكن أي من هذه الأشياء وعدًا ، فسيتم تمريره إلى الصفيف الناتج "كما هو".
104
104
105
-
For instance, here the results are `[1, 2, 3]`:
105
+
على سبيل المثال ، هنا النتائج هي "[1 ، 2 ، 3]`:
106
106
107
107
```js run
108
108
Promise.all([
@@ -114,14 +114,14 @@ Promise.all([
114
114
]).then(alert); // 1, 2, 3
115
115
```
116
116
117
-
So we are able to pass ready values to `Promise.all` where convenient.
117
+
حتى نتمكن من تمرير القيم الجاهزة إلى "Promise.all" حيثما يكون ذلك مناسبًا.
118
118
````
119
119
120
120
## Promise.allSettled
121
121
122
122
[recent browser="new"]
123
123
124
-
`Promise.all` rejects as a whole if any promise rejects. That's good for "all or nothing" cases, when we need *all* results successful to proceed:
124
+
`Promise.all` rيخرج ككل إذا رفض أي وعد. هذا أمر جيد بالنسبة لحالات "الكل أو لا شيء" ، عندما نحتاج إلى * جميع * النتائج الناجحة للمتابعة:
125
125
126
126
```js
127
127
Promise.all([
@@ -131,14 +131,15 @@ Promise.all([
131
131
]).then(render); // render method needs results of all fetches
132
132
```
133
133
134
-
`Promise.allSettled` just waits for all promises to settle, regardless of the result. The resulting array has:
134
+
`Promise.allSettled` just ينتظر جميع الوعود بتسوية بغض النظر عن النتيجة. المصفوفة الناتجة لها:
135
135
136
-
- `{status:"fulfilled", value:result}` for successful responses,
137
-
- `{status:"rejected", reason:error}` for errors.
136
+
- `{الحالة:" تم الوفاء "، القيمة: النتيجة}` للاستجابات الناجحة ،
137
+
- `{الحالة:" مرفوض "، السبب: خطأ}` للأخطاء.
138
138
139
-
For example, we'd like to fetch the information about multiple users. Even if one request fails, we're still interested in the others.
139
+
على سبيل المثال ، نود جلب المعلومات حول عدة مستخدمين. حتى لو فشل طلب واحد ، ما زلنا مهتمين بالطلبات الأخرى.
@@ -169,11 +171,11 @@ The `results` in the line `(*)` above will be:
169
171
]
170
172
```
171
173
172
-
So for each promise we get its status and `value/error`.
174
+
لذلك لكل promise نحصل على حالته و "القيمة / الخطأ".
173
175
174
176
### Polyfill
175
177
176
-
If the browser doesn't support `Promise.allSettled`, it's easy to polyfill:
178
+
إذا كان المتصفح لا يدعم `Promise.allSettled` ، فمن السهل إعادة الملء:
177
179
178
180
```js
179
181
if(!Promise.allSettled) {
@@ -189,17 +191,17 @@ if(!Promise.allSettled) {
189
191
}
190
192
```
191
193
192
-
In this code, `promises.map` takes input values, turns them into promises (just in case a non-promise was passed) with `p => Promise.resolve(p)`, and then adds `.then` handler to every one.
194
+
في هذا الرمز ، يأخذ `promises.map` قيم الإدخال ، ويحولها إلى وعود (فقط في حالة تمرير عدم الوعد) مع` p => Promise.resolve(p) `، ثم يضيف معالج` .then` إلى كل واحد.
193
195
194
-
That handler turns a successful result `value` into `{state:'fulfilled', value}`, and an error `reason` into `{state:'rejected', reason}`. That's exactly the format of `Promise.allSettled`.
196
+
يحول هذا المعالج نتيجة "القيمة" الناجحة إلى "{state:" الوفاء "و value}` والخطأ "reason" إلى "{state: "رفض"، reason}`. هذا هو بالضبط تنسيق `Promise.allSettled`.
195
197
196
-
Now we can use `Promise.allSettled` to get the results of *all* given promises, even if some of them reject.
198
+
الآن يمكننا استخدام "Promise.allSettled" للحصول على نتائج * جميع * الوعود المعطاة ، حتى لو رفض بعضها.
197
199
198
200
## Promise.race
199
201
200
-
Similar to `Promise.all`, but waits only for the first settled promise and gets its result (or error).
202
+
يشبه `Promise.all` ، ولكنه ينتظر فقط الوعد المستقر الأول ويحصل على نتيجته (أو خطأ).
201
203
202
-
The syntax is:
204
+
الصيغة هي:
203
205
204
206
```js
205
207
let promise = Promise.race(iterable);
@@ -215,28 +217,28 @@ Promise.race([
215
217
]).then(alert); // 1
216
218
```
217
219
218
-
The first promise here was fastest, so it became the result. After the first settled promise "wins the race", all further results/errors are ignored.
220
+
الوعد الأول هنا كان أسرع ، لذلك أصبح النتيجة. بعد الوعد المستقر الأول "يفوز بالسباق" ، يتم تجاهل جميع النتائج / الأخطاء الأخرى.
219
221
220
222
221
-
## Promise.resolve/reject
223
+
## Promise.resolve / reject
222
224
223
-
Methods `Promise.resolve` and `Promise.reject` are rarely needed in modern code, because `async/await` syntax (we'll cover it [a bit later](info:async-await)) makes them somewhat obsolete.
225
+
نادرًا ما تكون هناك حاجة إلى طريقتين `Promise.resolve` و` Promise.reject` في التعليمات البرمجية الحديثة ، لأن بناء الجملة `async / await` (سنغطيها [بعد قليل] (info:async-await)) يجعلها قديمة إلى حد ما.
224
226
225
-
We cover them here for completeness and for those who can't use `async/await` for some reason.
227
+
نحن نغطيها هنا للاكتمال ولأولئك الذين لا يستطيعون استخدام `` غير متزامن / انتظار '' لسبب ما.
226
228
227
-
### Promise.resolve
229
+
### promise.resolve
228
230
229
-
`Promise.resolve(value)` creates a resolved promise with the result `value`.
231
+
`Promise.resolve (القيمة)` يُنشئ وعدًا تم حله بالنتيجة `value`.
230
232
231
-
Same as:
233
+
مثل:
232
234
233
235
```js
234
236
let promise = new Promise(resolve => resolve(value));
235
237
```
236
238
237
-
The method is used for compatibility, when a function is expected to return a promise.
239
+
تُستخدم الطريقة للتوافق ، عندما يُتوقع أن تُرجع الدالة الوعد.
238
240
239
-
For example, the `loadCached` function below fetches a URL and remembers (caches) its content. For future calls with the same URL it immediately gets the previous content from cache, but uses `Promise.resolve` to make a promise of it, so the returned value is always a promise:
241
+
على سبيل المثال ، تجلب الوظيفة "loadCached" أدناه عنوان URL وتتذكر (تخبئ) محتواه. بالنسبة للمكالمات المستقبلية التي لها نفس عنوان URL ، تحصل على الفور على المحتوى السابق من ذاكرة التخزين المؤقت ، ولكنها تستخدم `Promise.resolve` لتقديم وعد بها ، لذا فإن القيمة التي تم إرجاعها هي دائمًا promise:
240
242
241
243
```js
242
244
let cache = new Map();
@@ -257,30 +259,30 @@ function loadCached(url) {
257
259
}
258
260
```
259
261
260
-
We can write `loadCached(url).then(…)`, because the function is guaranteed to return a promise. We can always use `.then` after `loadCached`. That's the purpose of `Promise.resolve` in the line `(*)`.
262
+
يمكننا كتابة "loadCached(url).then(…)` ، لأن الوظيفة مضمونة لإرجاع الوعد. يمكننا دائمًا استخدام `.then` بعد` loadCached`. هذا هو الغرض من "Promise.resolve" في السطر `(*)`.
261
263
262
-
### Promise.reject
264
+
### وعد.رفض
263
265
264
-
`Promise.reject(error)` creates a rejected promise with `error`.
266
+
`Promise.reject(error)` ينشئ وعدًا مرفوضًا بـ `error`.
265
267
266
-
Same as:
268
+
مثل:
267
269
268
270
```js
269
271
let promise = new Promise((resolve, reject) => reject(error));
270
272
```
271
273
272
-
In practice, this method is almost never used.
274
+
عمليًا ، لا يتم استخدام هذه الطريقة تقريبًا.
273
275
274
-
## Summary
276
+
## ملخص
275
277
276
-
There are 5 static methods of `Promise` class:
278
+
هناك 5 طرق ثابتة لفئة `Promise`:
277
279
278
-
1. `Promise.all(promises)` -- waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, it becomes the error of `Promise.all`, and all other results are ignored.
279
-
2. `Promise.allSettled(promises)` (recently added method) -- waits for all promises to settle and returns their results as an array of objects with:
280
-
- `state`: `"fulfilled"` or `"rejected"`
281
-
- `value` (if fulfilled) or `reason` (if rejected).
282
-
3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
283
-
4. `Promise.resolve(value)` -- makes a resolved promise with the given value.
284
-
5. `Promise.reject(error)` -- makes a rejected promise with the given error.
280
+
1. "Promise.all (الوعود)" - تنتظر جميع الوعود لحل وإرجاع مجموعة من نتائجها. إذا تم رفض أي من الوعود المعطاة ، يصبح خطأ "Promise.all" ، ويتم تجاهل جميع النتائج الأخرى.
281
+
2. "Promise.allSettled (الوعود)" (الطريقة المضافة حديثًا) - تنتظر جميع الوعود بتسوية نتائجها وإعادتها كمجموعة من الأشياء مع:
0 commit comments