Skip to content

Commit f17df35

Browse files
authored
Merge pull request #272 from akseyh/master
Promisification
2 parents 739063f + d41e131 commit f17df35

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
# Promisification
22

3-
Promisification -- is a long word for a simple transform. It's conversion of a function that accepts a callback into a function returning a promise.
3+
Promisification -- basit bir dönüşüm için uzun bir kelime. Bu callback kabul eden bir fonksiyonun promise dönen bir fonksiyona dönüştürülmesidir.
44

5-
To be more precise, we create a wrapper-function that does the same, internally calling the original one, but returns a promise.
5+
Daha kesin olmak gerekirse, aynı şeyi yapan, orjinali dahili olarak çağıran, fakat bir promise dönen bir sarmalayıcı fonksiyon oluşturuyoruz.
66

7-
Such transforms are often needed in real-life, as many functions and libraries are callback-based. But promises are more convenient. So it makes sense to promisify those.
7+
Birçok fonksiyon ve kütüphane callback-based olduğundan, bu tür dönüşümlere gerçek hayatta ihtiyaç duyulur.
88

9-
For instance, we have `loadScript(src, callback)` from the chapter <info:callbacks>.
9+
Örneğin, <info:callbacks> bölümünden `loadScript(src, callback)` var.
1010

1111
```js run
1212
function loadScript(src, callback) {
1313
let script = document.createElement('script');
1414
script.src = src;
1515

1616
script.onload = () => callback(null, script);
17-
script.onerror = () => callback(new Error(`Script load error for ${src}`));
17+
script.onerror = () => callback(new Error(`${src} için script yüklenme hatası`));
1818

1919
document.head.append(script);
2020
}
2121

22-
// usage:
22+
// kullanımı:
2323
// loadScript('path/script.js', (err, script) => {...})
2424
```
2525

26-
Let's promisify it. The new `loadScriptPromise(src)` function will do the same, but accept only `src` (no callback) and return a promise.
26+
Promisify yapalım. Yeni `loadScriptPromise(src)` fonksiyonu aynı şeyi yapacak, fakat sadece `src` (callback değil) kabul edecek ve bir promise dönecek.
2727

2828
```js
2929
let loadScriptPromise = function(src) {
@@ -35,60 +35,60 @@ let loadScriptPromise = function(src) {
3535
})
3636
}
3737

38-
// usage:
38+
// kulllanımı:
3939
// loadScriptPromise('path/script.js').then(...)
4040
```
4141

42-
Now `loadScriptPromise` fits well in our promise-based code.
42+
Artık `loadScriptPromise` promise-based kodumuza çok iyi uyuyor.
4343

44-
As we can see, it delegates all the work to the original `loadScript`, providing its own callback that translates to promise `resolve/reject`.
44+
Görebileceğimiz gibi, tüm işi orijinal `loadScript`e devrederek, `resolve/reject` promise'ına dönüşen kendi callback'ini sağlar.
4545

46-
As we may need to promisify many functions, it makes sense to use a helper.
46+
Pek çok fonksiyonu promisify etmemiz gerekebileceğinden bir helper kullanmak mantıklı olur.
4747

48-
That's actually very simple -- `promisify(f)` below takes a to-promisify function `f` and returns a wrapper function.
48+
Bu aslında çok basit -- `promisify(f)` bir to-promisify `f` fonksiyonu alır ve bir sarmalayıcı fonksiyonu döner.
4949

50-
That wrapper does the same as in the code above: returns a promise and passes the call to the original `f`, tracking the result in a custom callback:
50+
Bu sarmalayıcı yukarıdaki kodla aynı şeyi yapar: bir promise döndürür ve aramayı orijinal `f`e iletir, sonucu özel bir callback izler:
5151

5252
```js
5353
function promisify(f) {
54-
return function (...args) { // return a wrapper-function
54+
return function (...args) { // bir sarmalayıcı fonksiyon döner
5555
return new Promise((resolve, reject) => {
56-
function callback(err, result) { // our custom callback for f
56+
function callback(err, result) { // f için özel callback
5757
if (err) {
5858
return reject(err);
5959
} else {
6060
resolve(result);
6161
}
6262
}
6363

64-
args.push(callback); // append our custom callback to the end of arguments
64+
args.push(callback); // argümanların sonuna özel callback'imizi ekler
6565

66-
f.call(this, ...args); // call the original function
66+
f.call(this, ...args); // orijinal fonksiyonu çağırır
6767
});
6868
};
6969
};
7070

71-
// usage:
71+
// kullanımı:
7272
let loadScriptPromise = promisify(loadScript);
7373
loadScriptPromise(...).then(...);
7474
```
7575

76-
Here we assume that the original function expects a callback with two arguments `(err, result)`. That's what we encounter most often. Then our custom callback is in exactly the right format, and `promisify` works great for such a case.
76+
Burada orijinal fonksiyonun iki argümanlı bir callback beklediğini varsayıyoruz `(err, result)`. En sık karşılaştığımız şey bu. O zaman özel callback'imiz tam olarak doğru biçimdedir ve `promisify` böyle bir durum için harika çalışır.
7777

78-
But what if the original `f` expects a callback with more arguments `callback(err, res1, res2)`?
78+
Ama ya orijinal `f` daha fazla argümanlı bir callback bekliyorsa `callback(err, res1, res2)`?
7979

80-
Here's a modification of `promisify` that returns an array of multiple callback results:
80+
İşte bir dizi çoklu callback sonucu döndüren bir `promisify` değişikliği:
8181

8282
```js
83-
// promisify(f, true) to get array of results
83+
// bir dizi sonuç elde etmek için promisify(f, true)
8484
function promisify(f, manyArgs = false) {
8585
return function (...args) {
8686
return new Promise((resolve, reject) => {
87-
function *!*callback(err, ...results*/!*) { // our custom callback for f
87+
function *!*callback(err, ...results*/!*) { // f için özel callback'imiz
8888
if (err) {
8989
return reject(err);
9090
} else {
91-
// resolve with all callback results if manyArgs is specified
91+
// manyArgs belirtilirse tüm callback sonuçlarıyla çözümle
9292
*!*resolve(manyArgs ? results : results[0]);*/!*
9393
}
9494
}
@@ -100,19 +100,19 @@ function promisify(f, manyArgs = false) {
100100
};
101101
};
102102
103-
// usage:
103+
// kullanımı:
104104
f = promisify(f, true);
105105
f(...).then(arrayOfResults => ..., err => ...)
106106
```
107107
108-
In some cases, `err` may be absent at all: `callback(result)`, or there's something exotic in the callback format, then we can promisify such functions without using the helper, manually.
108+
Bazı durumlarda `err` olmayabilir: `callback(result)` veya callback biçiminde farklı bir şey varsa, bu tür fonksiyonları helper kullanmadan manuel olarak promisify edebiliriz.
109109
110-
There are also modules with a bit more flexible promisification functions, e.g. [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify). In Node.js, there's a built-in `util.promisify` function for that.
110+
Biraz daha esnek promisification fonksiyonlarına sahip modüller de vardır, örnek [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify). Node.js'de bunun için yerleşik bir `util.promisify` fonksiyonu vardır.
111111

112112
```smart
113-
Promisification is a great approach, especially when you use `async/await` (see the next chapter), but not a total replacement for callbacks.
113+
Promisification, özellikle `async/await` kullandığınızda harika bir yaklaşımdır (sonraki bölüme bakın), ancak callbacklerin tam olarak yerine geçmez.
114114
115-
Remember, a promise may have only one result, but a callback may technically be called many times.
115+
Unutmayın, bir promise yalnızca bir sonuca sahip olabilir, ancak bir callback teknik olarak birçok kez çağrılabilir.
116116
117-
So promisification is only meant for functions that call the callback once. Further calls will be ignored.
117+
Bu nedenle, promisification yalnızca callback'i bir kez çağıran fonksiyonlar içindir. Diğer çağırmalar göz ardı edilecektir.
118118
```

0 commit comments

Comments
 (0)