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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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.
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.
To be more precise, we create a wrapper-function that does the same, internally calling the original one, but returns a promise.
Daha kesin olmak gerekirse, aynı şeyi yapan, orjinali dahili olarak çağıran, fakat bir promise dönen bir sarmalayıcı fonksiyon oluşturuyoruz.
Such transforms are often needed in real-life, as many functions and libraries arecallback-based. But promises are more convenient. So it makes sense to promisify those.
Birçok fonksiyon ve kütüphanecallback-based olduğundan, bu tür dönüşümlere gerçek hayatta ihtiyaç duyulur.
For instance, we have `loadScript(src, callback)`from the chapter <info:callbacks>.
Örneğin, <info:callbacks> bölümünden `loadScript(src, callback)`var.
Here we assume that the original function expects acallbackwith 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.
Burada orijinal fonksiyonun iki argümanlı bircallbackbeklediğ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.
But what if the original`f`expects a callback with more arguments `callback(err, res1, res2)`?
Ama ya orijinal`f`daha fazla argümanlı bir callback bekliyorsa `callback(err, res1, res2)`?
Here's a modification of `promisify` that returns an array of multiple callback results:
İşte bir dizi çoklu callback sonucu döndüren bir `promisify` değişikliği:
```js
// promisify(f, true)to get array of results
//bir dizi sonuç elde etmek içinpromisify(f, true)
function promisify(f, manyArgs = false) {
return function (...args) {
return new Promise((resolve, reject) => {
function *!*callback(err, ...results*/!*) { //our custom callback for f
function *!*callback(err, ...results*/!*) { //f için özel callback'imiz
if (err) {
return reject(err);
} else {
//resolve with all callbackresults if manyArgs is specified
//manyArgs belirtilirse tüm callbacksonuçlarıyla çözümle
*!*resolve(manyArgs ? results : results[0]);*/!*
}
}
Expand All
@@ -100,19 +100,19 @@ function promisify(f, manyArgs = false) {
};
};
//usage:
//kullanımı:
f = promisify(f, true);
f(...).then(arrayOfResults => ..., err => ...)
```
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.
Bazı durumlarda`err`olmayabilir: `callback(result)` veya callback biçiminde farklı bir şey varsa, bu tür fonksiyonları helper kullanmadan manuel olarak promisify edebiliriz.
There are also modules with a bit more flexible promisification functions, e.g. [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify).InNode.js, there's a built-in`util.promisify`function for that.
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.
```smart
Promisification is a great approach, especially when you use`async/await`(see the next chapter),but not a total replacement for callbacks.
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.
Remember, a promisemay have only one result, but a callbackmay technically be called many times.
Unutmayın, bir promiseyalnızca bir sonuca sahip olabilir, ancak bir callbackteknik olarak birçok kez çağrılabilir.
Sopromisificationis only meant for functions that call the callback once. Further calls will be ignored.
Bu nedenle,promisificationyalnızca callback'i bir kez çağıran fonksiyonlar içindir. Diğer çağırmalar göz ardı edilecektir.
```
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.