Skip to content

Commit ee078ad

Browse files
committed
Promise API
1 parent ef9d1e5 commit ee078ad

1 file changed

Lines changed: 64 additions & 62 deletions

File tree

1-js/11-async/05-promise-api/article.md

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# Promise API
1+
# Promise API
22

3-
There are 5 static methods in the `Promise` class. We'll quickly cover their use cases here.
3+
هناك خمس دوال ثابتة في `Promise` class. سنغطي بسرعة حالات استخدامهم هنا.
44

55
## Promise.all
66

7-
Let's say we want many promises to execute in parallel and wait until all of them are ready.
7+
لنفترض أننا نريد تنفيذ العديد من الوعود بالتوازي والانتظار حتى تصبح جميعها جاهزة.
88

9-
For instance, download several URLs in parallel and process the content once they are all done.
9+
على سبيل المثال ، قم بتنزيل العديد من عناوين URL بالتوازي وقم بمعالجة المحتوى بمجرد الانتهاء منها.
1010

11-
That's what `Promise.all` is for.
11+
هذا هو الغرض من "Promise.all".
1212

13-
The syntax is:
13+
يكتب على النحو التالي:
1414

1515
```js
1616
let promise = Promise.all([...promises...]);
1717
```
1818

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` مجموعة من الوعود (من الناحية التقنية يمكن أن تكون قابلة للتكرار ، ولكن عادة ما تكون صفيفًا) ويعيد وعدًا جديدًا.
2020

21-
The new promise resolves when all listed promises are settled, and the array of their results becomes its result.
21+
يتم حل الوعد الجديد عندما تتم تسوية جميع الوعود المدرجة ، وتصبح مجموعة نتائجها نتاجها.
2222

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]`:
2424

2525
```js run
2626
Promise.all([
@@ -30,11 +30,11 @@ Promise.all([
3030
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
3131
```
3232

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+
يرجى ملاحظة أن ترتيب أعضاء الصفيف الناتج هو نفسه كما في وعود المصدر. على الرغم من أن الوعد الأول يستغرق وقتًا أطول للحل ، إلا أنه لا يزال الأول في مجموعة النتائج.
3434

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`.
3636

37-
For instance, if we have an array of URLs, we can fetch them all like this:
37+
على سبيل المثال ، إذا كان لدينا مجموعة من عناوين URL ، فيمكننا جلبها جميعًا مثل هذا:
3838

3939
```js run
4040
let urls = [
@@ -53,7 +53,7 @@ Promise.all(requests)
5353
));
5454
```
5555

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 بأسمائهم (يمكننا جلب مجموعة من السلع حسب معرفاتهم ، المنطق متطابق):
5757

5858
```js run
5959
let names = ['iliakan', 'remy', 'jeresig'];
@@ -75,9 +75,9 @@ Promise.all(requests)
7575
.then(users => users.forEach(user => alert(user.name)));
7676
```
7777

78-
**If any of the promises is rejected, the promise returned by `Promise.all` immediately rejects with that error.**
78+
** إذا تم رفض أي من الوعود ، فإن الوعد الذي تم إرجاعه بواسطة "Promise.all" يرفض على الفور هذا الخطأ. **
7979

80-
For instance:
80+
على سبيل المثال:
8181

8282
```js run
8383
Promise.all([
@@ -89,20 +89,20 @@ Promise.all([
8989
]).catch(alert); // Error: Whoops!
9090
```
9191

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" بالكامل.
9393

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` على الفور ، متناسين تمامًا الوعود الأخرى في القائمة. يتم تجاهل نتائجهم.
9696
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+
على سبيل المثال ، إذا كان هناك العديد من مكالمات `الجلب` ، كما هو موضح في المثال أعلاه ، وفشل أحدها ، فسيستمر الآخرون في التنفيذ ، ولكن لن يُشاهدهم" الوعد. جميع "بعد الآن. ربما سيستقرون ، ولكن سيتم تجاهل نتائجهم.
9898
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.
100100
```
101101

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 (...)` الوعود (في معظم الحالات مجموعة) من الوعود. ولكن إذا لم يكن أي من هذه الأشياء وعدًا ، فسيتم تمريره إلى الصفيف الناتج "كما هو".
104104

105-
For instance, here the results are `[1, 2, 3]`:
105+
على سبيل المثال ، هنا النتائج هي "[1 ، 2 ، 3]`:
106106

107107
```js run
108108
Promise.all([
@@ -114,14 +114,14 @@ Promise.all([
114114
]).then(alert); // 1, 2, 3
115115
```
116116

117-
So we are able to pass ready values to `Promise.all` where convenient.
117+
حتى نتمكن من تمرير القيم الجاهزة إلى "Promise.all" حيثما يكون ذلك مناسبًا.
118118
````
119119
120120
## Promise.allSettled
121121
122122
[recent browser="new"]
123123
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يخرج ككل إذا رفض أي وعد. هذا أمر جيد بالنسبة لحالات "الكل أو لا شيء" ، عندما نحتاج إلى * جميع * النتائج الناجحة للمتابعة:
125125
126126
```js
127127
Promise.all([
@@ -131,14 +131,15 @@ Promise.all([
131131
]).then(render); // render method needs results of all fetches
132132
```
133133
134-
`Promise.allSettled` just waits for all promises to settle, regardless of the result. The resulting array has:
134+
`Promise.allSettled` just ينتظر جميع الوعود بتسوية بغض النظر عن النتيجة. المصفوفة الناتجة لها:
135135
136-
- `{status:"fulfilled", value:result}` for successful responses,
137-
- `{status:"rejected", reason:error}` for errors.
136+
- `{الحالة:" تم الوفاء "، القيمة: النتيجة}` للاستجابات الناجحة ،
137+
- `{الحالة:" مرفوض "، السبب: خطأ}` للأخطاء.
138138
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+
على سبيل المثال ، نود جلب المعلومات حول عدة مستخدمين. حتى لو فشل طلب واحد ، ما زلنا مهتمين بالطلبات الأخرى.
140+
141+
دعنا نستخدم `Promise.allSettled`:
140142
141-
Let's use `Promise.allSettled`:
142143
143144
```js run
144145
let urls = [
@@ -160,7 +161,8 @@ Promise.allSettled(urls.map(url => fetch(url)))
160161
});
161162
```
162163
163-
The `results` in the line `(*)` above will be:
164+
ستكون "النتائج" في السطر `(*)` أعلاه:
165+
164166
```js
165167
[
166168
{status: 'fulfilled', value: ...response...},
@@ -169,11 +171,11 @@ The `results` in the line `(*)` above will be:
169171
]
170172
```
171173
172-
So for each promise we get its status and `value/error`.
174+
لذلك لكل promise نحصل على حالته و "القيمة / الخطأ".
173175
174176
### Polyfill
175177
176-
If the browser doesn't support `Promise.allSettled`, it's easy to polyfill:
178+
إذا كان المتصفح لا يدعم `Promise.allSettled` ، فمن السهل إعادة الملء:
177179
178180
```js
179181
if(!Promise.allSettled) {
@@ -189,17 +191,17 @@ if(!Promise.allSettled) {
189191
}
190192
```
191193
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` إلى كل واحد.
193195
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`.
195197
196-
Now we can use `Promise.allSettled` to get the results of *all* given promises, even if some of them reject.
198+
الآن يمكننا استخدام "Promise.allSettled" للحصول على نتائج * جميع * الوعود المعطاة ، حتى لو رفض بعضها.
197199
198200
## Promise.race
199201
200-
Similar to `Promise.all`, but waits only for the first settled promise and gets its result (or error).
202+
يشبه `Promise.all` ، ولكنه ينتظر فقط الوعد المستقر الأول ويحصل على نتيجته (أو خطأ).
201203
202-
The syntax is:
204+
الصيغة هي:
203205
204206
```js
205207
let promise = Promise.race(iterable);
@@ -215,28 +217,28 @@ Promise.race([
215217
]).then(alert); // 1
216218
```
217219
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+
الوعد الأول هنا كان أسرع ، لذلك أصبح النتيجة. بعد الوعد المستقر الأول "يفوز بالسباق" ، يتم تجاهل جميع النتائج / الأخطاء الأخرى.
219221
220222
221-
## Promise.resolve/reject
223+
## Promise.resolve / reject
222224
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)) يجعلها قديمة إلى حد ما.
224226
225-
We cover them here for completeness and for those who can't use `async/await` for some reason.
227+
نحن نغطيها هنا للاكتمال ولأولئك الذين لا يستطيعون استخدام `` غير متزامن / انتظار '' لسبب ما.
226228
227-
### Promise.resolve
229+
### promise.resolve
228230
229-
`Promise.resolve(value)` creates a resolved promise with the result `value`.
231+
`Promise.resolve (القيمة)` يُنشئ وعدًا تم حله بالنتيجة `value`.
230232
231-
Same as:
233+
مثل:
232234
233235
```js
234236
let promise = new Promise(resolve => resolve(value));
235237
```
236238
237-
The method is used for compatibility, when a function is expected to return a promise.
239+
تُستخدم الطريقة للتوافق ، عندما يُتوقع أن تُرجع الدالة الوعد.
238240
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:
240242
241243
```js
242244
let cache = new Map();
@@ -257,30 +259,30 @@ function loadCached(url) {
257259
}
258260
```
259261
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" في السطر `(*)`.
261263
262-
### Promise.reject
264+
### وعد.رفض
263265
264-
`Promise.reject(error)` creates a rejected promise with `error`.
266+
`Promise.reject (error)` ينشئ وعدًا مرفوضًا بـ `error`.
265267
266-
Same as:
268+
مثل:
267269
268270
```js
269271
let promise = new Promise((resolve, reject) => reject(error));
270272
```
271273
272-
In practice, this method is almost never used.
274+
عمليًا ، لا يتم استخدام هذه الطريقة تقريبًا.
273275
274-
## Summary
276+
## ملخص
275277
276-
There are 5 static methods of `Promise` class:
278+
هناك 5 طرق ثابتة لفئة `Promise`:
277279
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 (الوعود)" (الطريقة المضافة حديثًا) - تنتظر جميع الوعود بتسوية نتائجها وإعادتها كمجموعة من الأشياء مع:
282+
- `الدولة`:` `محقق '' أو` `مرفوض ''
283+
- "القيمة" (إذا تحققت) أو "السبب" (إذا تم رفضها).
284+
3. "الوعد" (الوعود) - ينتظر الوعد الأول بالاستقرار ، وتصبح نتيجته / خطأه النتيجة.
285+
4. "Promise.resolve (القيمة)" - يقدم وعدًا ثابتًا بقيمة معينة.
286+
5. "Promise.reject (خطأ)" - يقدم وعدًا مرفوضًا مع الخطأ المحدد.
285287
286-
Of these five, `Promise.all` is probably the most common in practice.
288+
من بين هذه العناصر الخمسة ، يعد "Promise.all" الأكثر شيوعًا في الممارسة.

0 commit comments

Comments
 (0)