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
There are 5 static methods in the `Promise` class. We'll quickly cover their use cases here.
3
+
`Promise`de 5 statik yöntem vardır. Kullanım davalarını burada hızlı bir şekilde ele alacağız.
4
4
5
5
## Promise.resolve
6
6
7
-
The syntax:
7
+
Söz dizimi:
8
8
9
9
```js
10
10
let promise =Promise.resolve(value);
11
11
```
12
12
13
-
Returns a resolved promise with the given `value`.
14
-
15
-
Same as:
13
+
Verilen `value` ile çözülmüş olan bir söz verir.
14
+
Buradaki gibi:
16
15
17
16
```js
18
17
let promise =newPromise(resolve=>resolve(value));
19
18
```
20
19
21
-
The method is used when we already have a value, but would like to have it "wrapped" into a promise.
20
+
Yöntem zaten bir değere sahipken kullanılır. Ancal bir söz içine "sarılmış" olmasını ister.
22
21
23
-
For instance, the `loadCached`function below fetches the `url` and remembers the result, so that future calls on the same URL return it immediately:
22
+
Örneğin, `loadCached`fonksiyonu aşağıda `url`yi alır ve sonucu hatırlar. Böylece aynı URL'deki gelecekteki çağrılar hemen döndürülür.
24
23
25
24
```js
26
25
functionloadCached(url) {
@@ -41,59 +40,60 @@ function loadCached(url) {
41
40
}
42
41
```
43
42
44
-
We can use `loadCached(url).then(…)`, because the function is guaranteed to return a promise. That's the purpose `Promise.resolve`serves in the line `(*)`: it makes sure the interface is unified. We can always use `.then` after `loadCached`.
43
+
`loadCached(url).then(…)` kullanabiliriz. Çünkü fonksiyonun bir söz döndürmesi garantilidir. Amaç `Promise.resolve``(*)` doğrultusunda hizmet eder: Arayüzünün birleşik olduğundan emin olun. `.then`den sonra her zaman `loadCached` kullanabiliriz.
45
44
46
45
## Promise.reject
47
46
48
-
The syntax:
47
+
Söz dizimi:
49
48
50
49
```js
51
50
let promise =Promise.reject(error);
52
51
```
53
52
54
-
Create a rejected promise with the `error`.
53
+
`error` ile reddedilen bir söz oluşturun.
55
54
56
-
Same as:
55
+
Yukarıdaki ile aynı:
57
56
58
57
```js
59
58
let promise =newPromise((resolve, reject) =>reject(error));
60
59
```
61
60
62
-
We cover it here for completeness, rarely used in real code.
61
+
Gerçek kodda nadiren kullanılan, bütünlük için buradayız.
63
62
64
63
## Promise.all
65
64
66
-
Let's say we want to run many promises to execute in parallel, and wait till all of them are ready.
65
+
Paralel olarak yürütülmek için birçok söz vermek isteriz ve hepsinin hazır olmasını bekleriz.
67
66
68
-
For instance, download several URLs in parallel and process the content when all are done.
67
+
Örneğin, paralel olarak birkaç URL'yi indirin ve hepsi bittiğinde içeriği işleyin.
69
68
70
-
That's what`Promise.all` is for.
69
+
Bunun için`Promise.all`.
71
70
72
-
The syntax is:
71
+
Söz dizimi:
73
72
74
73
```js
75
74
let promise =Promise.all([...promises...]);
76
75
```
77
76
78
-
It takes an array of promises (technically can be any iterable, but usually an array) and returns a new promise.
77
+
Yeni bir söz alır ve bir dizi söz alır (technically can be any iterable, but usually an array.)
79
78
80
-
The new promise resolves when all listed promises are settled and has an array of their results.
79
+
Yeni söz, listelenen tüm sözlerin yerine getirildiği ve sonuçların bir dizisine sahip olduğunda karar verir.
81
80
82
-
For instance, the`Promise.all`below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
81
+
Örneğin, aşağıdaki`Promise.all`3 saniye sonra yerleşir ve sonucu `[1, 2, 3]` dizisidir:
]).then(alert); // 1,2,3 when promises are ready: each promise contributes an array member
88
+
]).then(alert); // 1,2,3 sözler hazır olduğunda: her söz bir dizi üyesine katkıda bulunur
90
89
```
91
90
92
-
Please note that the relative order is the same. Even though the first promise takes the longest time to resolve, it is still first in the array of results.
91
+
Lütfen göreli siparişin aynı olduğunu unutmayın. İlk sözün sözülmesi uzun sürse bile sonuçta ilk sırada yer almaktadır.
92
+
93
+
Yayın bir hile, bir dizi iş verisini bir dizi sözle eşleştirmek ve ardından bunu `Promise.all` içine kaydırmaktır.
93
94
94
-
A common trick is to map an array of job data into an array of promises, and then wrap that into `Promise.all`.
95
+
Örneğin, eğer bir dizi URL'miz varsa hepsini şöyle getirebiliriz:
95
96
96
-
For instance, if we have an array of URLs, we can fetch them all like this:
A bigger example with fetching user information for an array of github users by their names (or we could fetch an array of goods by their ids, the logic is same):
115
+
Bir dizi github kullanıcısı için kullanıcı bilgilerini adlarına göre almakla ilgili daha büyük bir örnek (veya bir mal dizisini kimlikleriyle alabiliriz. Mantık aynıdır):
116
+
116
117
117
118
```js run
118
119
let names = ['iliakan', 'remy', 'jeresig'];
@@ -121,22 +122,22 @@ let requests = names.map(name => fetch(`https://api.github.com/users/${name}`));
121
122
122
123
Promise.all(requests)
123
124
.then(responses=> {
124
-
//all responses are ready, we can show HTTP status codes
125
+
//Tüm cevaplar hazır. HTTP durum kodlarını gösterebiliriz
125
126
for(let response of responses) {
126
-
alert(`${response.url}: ${response.status}`); //shows 200 for every url
127
+
alert(`${response.url}: ${response.status}`); //Her URL için 200 gösterir
127
128
}
128
129
129
130
return responses;
130
131
})
131
-
//map array of responses into array of response.json() to read their content
132
+
//Yanıt dizisini, içeriğini okumak için response.json() dizisine eşleyin
**If any of the promises is rejected,`Promise.all`immediately rejects with that error.**
138
+
**Eğer sözlerden herhangi biri ret edildiyse `Promise.all`bu hatayı hemen ret eder**
138
139
139
-
For instance:
140
+
Örneğin:
140
141
141
142
```js run
142
143
Promise.all([
@@ -148,57 +149,59 @@ Promise.all([
148
149
]).catch(alert); // Error: Whoops!
149
150
```
150
151
151
-
Here the second promise rejects in two seconds. That leads to immediate rejection of `Promise.all`, so `.catch` executes: the rejection error becomes the outcome of the whole `Promise.all`.
152
+
İşte ikinci söz iki saniye içinde reddediyor. Bu `Promise.all`un hemen reddedilmesine yol açar, bu yüzden `.catch` çalıştırır: reddedilme hatası tüm `Promise.all`un sonucudur.
153
+
152
154
153
155
```warn header="In case of an error, other promises are ignored"
154
-
If one promise rejects, `Promise.all` immediately rejects, completely forgetting about the other ones in the list. Their results are ignored.
156
+
Eğer bir söz reddederse, `Promise.all` derhal reddeder. Listedeki diğerlerini tamamen unutur. Onların sonuçları göz ardı edilir.
155
157
156
-
For example, if there are multiple `fetch` calls, like in the example above, and one fails, other ones will still continue to execute, but `Promise.all` don't watch them any more. They will probably settle, but the result will be ignored.
158
+
Örneğin, yukarıdaki örnekte olduğu gibi birden fazla `fetch` çağrısı varsa ve biri başarısız olursa diğeri hala yürütülmeye devam eder. Ancak `Promise.all` artık onları izlememektedir. Muhtemelen yerleşecekler ancak sonuç göz ardı edilecektir.
157
159
158
-
`Promise.all` does nothing to cancel them, as there's no concept of "cancellation" in promises. In [another chapter](fetch-abort) we'll cover `AbortController` that aims to help with that, but it's not a part of the Promise API.
160
+
`Promise.all` sözlerinde "iptal" kavramı olmadığı için onları iptal edecek hiçbir şey yapmaz. [Başka bir bölümde](fetch-abort) bu konuda yardımcı olmayı amaçlayan `AbortController`ı ele alacağız. Ancak bu Promise API'sinin bir parçası değil.
159
161
```
160
162
161
163
````smart header="`Promise.all(...)` allows non-promise items in `iterable`"
162
-
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 wrapped in `Promise.resolve`.
164
+
Normalde, `Promise.all(...)` sözlerin yenilenebilir (çoğu durumda bir dizi) kabul eder. Ancak bu nesnelerden herhangi biri bir söz değilse `Promise.respove` içine sarılır.
165
+
```
163
166
164
-
For instance, here the results are `[1, 2, 3]`:
167
+
Örneğin burada `[1, 2, 3]` döner:
165
168
166
169
```js run
167
170
Promise.all([
168
171
new Promise((resolve, reject) => {
169
172
setTimeout(() => resolve(1), 1000)
170
173
}),
171
-
2, //treated as Promise.resolve(2)
172
-
3//treated as Promise.resolve(3)
174
+
2, // Promise.resolve(2) olarak kabul edildi.
175
+
3 // Promise.resolve(3) olarak kabul edildi.
173
176
]).then(alert); // 1, 2, 3
174
177
```
175
178
176
-
So we are able to pass non-promise values to `Promise.all` where convenient.
179
+
Bu yüzden uygun olmayan durumlarda `Promise.all`a söz etmeyen değerleri aktarabiliriz.
177
180
178
181
````
179
182
180
183
## Promise.allSettled
181
184
182
185
[recent browser="new"]
183
186
184
-
`Promise.all` rejects as a whole if any promise rejects. That's good in cases, when we need *all* results to go on:
187
+
Herhangi bir söz reddederse `Promise.all` bir bütün olarak eder. Devam etmek için *all* sonuçlarına ihtiyacımız olduğunda bu iyidir:
185
188
186
189
```js
187
190
Promise.all([
188
191
fetch('/template.html'),
189
192
fetch('/style.css'),
190
193
fetch('/data.json')
191
-
]).then(render); // render method needs them all
194
+
]).then(render); // render yöntemi hepsine ihtiyaç duyuyor
192
195
```
193
196
194
-
`Promise.allSettled` waits for all promises to settle: even if one rejects, it waits for the others. The resulting array has:
197
+
`Promise.allSettled` tüm sözlerin yerine getirilmesini bekler: biri reddetse bile diğerini bekler. Sonuçta ortaya çıkan dizin:
195
198
196
-
- `{status:"fulfilled", value:result}` for successful responses,
197
-
- `{status:"rejected", reason:error}` for errors.
199
+
- `{status:"fulfilled", value:result}` başarılı cevap için,
@@ -229,11 +232,11 @@ The `results` in the line `(*)` above will be:
229
232
]
230
233
```
231
234
232
-
So, for each promise we get its status and `value/reason`.
235
+
Dolayısıyla, her söz için onun satütüsünü ve `değer/sebep` bilgisini alırız.
233
236
234
237
### Polyfill
235
238
236
-
If the browser doesn't support `Promise.allSettled`, it's easy to polyfill:
239
+
Eğer tarayıcı `Promise.allSettled` özelliğini desteklemiyorsa, polyfill kolaydır.
237
240
238
241
```js
239
242
if(!Promise.allSettled) {
@@ -249,23 +252,23 @@ if(!Promise.allSettled) {
249
252
}
250
253
```
251
254
252
-
In this code, `promises.map` takes input values, turns into promises (just in case a non-promise was passed) with `p => Promise.resolve(p)`, and then adds `.then` handler to it.
255
+
Bu kodda, `promises.map` giriş değerini alır, `p => Promise.resolve(p)` ile sözleri döndürüyor (sadece bir söz verilmemişse) ve sonra bunu işleyiciye ekler.
253
256
254
-
That handler turns a successful result `v` into `{state:'fulfilled', value:v}`, and an error `r` into `{state:'rejected', reason:r}`. That's exactly the format of `Promise.allSettled`.
257
+
Bu işleyici başarılı bir `v` sonucusunu `{state:'fulfilled', value:v}` ve bir `r` hatasını `{state:'rejected', reason:r}` olarak çevirir. Bu `Promise.allSettled` formatıyla aynıdır.
255
258
256
-
Then we can use `Promise.allSettled` to get the results or *all* given promises, even if some of them reject.
259
+
Sonra bazı sonuçları reddetse bile sonuçları almak ya da *all* sözleri vermek için `Promise.allSettled`i kullanabiliriz.
257
260
258
261
## Promise.race
259
262
260
-
Similar to `Promise.all`, it takes an iterable of promises, but instead of waiting for all of them to finish, it waits for the first result (or error), and goes on with it.
263
+
`Promise.all` benzer şekilde sözler yenilenebilir. Ancak hepsinin bitmesini beklemek yerine ilk sonucu (veya hatayı) bekler ve devam eder.
261
264
262
-
The syntax is:
265
+
Söz dizimi:
263
266
264
267
```js
265
268
let promise = Promise.race(iterable);
266
269
```
267
270
268
-
For instance, here the result will be `1`:
271
+
Mesela burada sonuç `1` olacak:
269
272
270
273
```js run
271
274
Promise.race([
@@ -275,18 +278,17 @@ Promise.race([
275
278
]).then(alert); // 1
276
279
```
277
280
278
-
So, the first result/error becomes the result of the whole `Promise.race`. After the first settled promise "wins the race", all further results/errors are ignored.
279
-
280
-
## Summary
281
+
Böylece ilk sonuç/hata bütün `Promise.race` sonucu olur. İlk kararlaştırılan sözün ardından "yarışı kazanır", diğer tüm sonuçlar/hatalar göz ardı edilir
281
282
282
-
There are 5 static methods of `Promise` class:
283
+
## Özetle
283
284
284
-
1. `Promise.resolve(value)` -- makes a resolved promise with the given value.
285
-
2. `Promise.reject(error)` -- makes a rejected promise with the given error.
286
-
3. `Promise.all(promises)` -- waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, then it becomes the error of `Promise.all`, and all other results are ignored.
287
-
4. `Promise.allSettled(promises)` (a new method) -- waits for all promises to resolve or reject and returns an array of their results as object with:
288
-
- `state`: `'fulfilled'` or `'rejected'`
289
-
- `value` (if fulfilled) or `reason` (if rejected).
290
-
5. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
285
+
`Promise` sınıfının 5 statik metodu vardır:
291
286
292
-
Of these five, `Promise.all/allSettled` are the most common in practice.
287
+
1. `Promise.resolve(value)` -- verilen değerle çözümlenmiş bir söz verir.
288
+
2. `Promise.reject(error)` -- verilen hata ile reddedilen bir söz verir..
289
+
3. `Promise.all(promises)` -- çözmek için tüm sözleri bekler ve sonuçlarının bir dizisini döndürür. Eğer verilen sözlerden herhangi biri reddederse o zaman `Promise.all` hatası olur ve diğer tüm sonuçlar göz ardı edilir.
290
+
4. `Promise.allSettled(promises)` (yeni bir metod) -- tüm sözlerin çözülmesini veya reddedilmesini bekler ve sonuçlarının bir dizisini nesne olarak döndürür.
291
+
- `state`: `'fulfilled'` yada `'rejected'`
292
+
- `value` (if fulfilled) ya da `reason` (reddedilirse).
293
+
5. `Promise.race(promises)` -- ilk yerlmeşmeye söz vermek için bekler ve sonucu/hatası sonuç olur.
294
+
Bu beş maddede `Promise.all/allSettled` en yaygın kullanılanıdır.
0 commit comments