Skip to content

Instantly share code, notes, and snippets.

@ysfzrn
Created March 8, 2017 14:46
Show Gist options
  • Save ysfzrn/1ccdff01985f5b4a4290a34ae07d6a7b to your computer and use it in GitHub Desktop.
Save ysfzrn/1ccdff01985f5b4a4290a34ae07d6a7b to your computer and use it in GitHub Desktop.
callbacketc
Trying to Save Callbacks
analytics.trackPurchase(purchaseData,function(){
tracked=true;
chargeCreditCard();
displayThankyouPage();
}
);
Üstteki kodda bide baktık ki bir sabah, kredi kartından tam 5 kere ücret alınmış
biz de aşağıdaki kod satırını ekledik.
var tracked=false;
analytics.trackPurchase(purchaseData,function(){
if(!tracked){
tracked=true;
chargeCreditCard();
displayThankyouPage();
}}
);
Çözdük mü problemi bence hayır.Çünkü;
- Callback çok erken çağrılabilir (tracked daha set edilmeden)
- Callback çok geç ya da hiç çağrılmayabilir
- Gereksiz ya da yanlış parametre geçersek ne olacak
- Hatalar, exceptionlar etc.
SPLIT CALLBACKS
--------------------
function success(data){
console.log(data);
}
function failure(err){
console.error(err);
}
ajax("http://some.url.1",success,failure);
ERROR-FIRST-STYLE (NODE STYLE)
-------------------------
function response(err,data){
//error?
if(err){
console.error(err);
}
//otherwise,assumesuccess
else{
console.log(data);
}
}
ajax("http://some.url.1",response);
-First,it has not really resolved the majority of trust issues like it may appear.
There is nothing about either callback that prevents or filters unwanted repeated invocations.
Moreover,things are worse now,because you may get both success and error signals, or
neither, and you still have to code around either of those conditions.
-What about trust issue of never being called? If this is a concern, you likely will need to set up
a timeout cancels the event. You could make a utility to help you with that:
PROOF-OF-CONCEPT
---------------------------
function timeoutify(fn,delay){
var intv=setTimeout(function(){
intv=null;
fn(newError("Timeout!"));
},delay);
return function(){
//timeout hasn't happened yet ?
if(intv){
clearTimeout(intv);
fn.apply(this,arguments);
}};
}
function foo(err,data){
//error?
if(err){
console.error(err);
}
//otherwise,assumesuccess
else{
console.log(data);
}
}
ajax("http://some.url.1",timeoutify(foo, 500) );
---------------------------------------------------
----------------******************-----------------------------
- Callback çok erken çağrılabilir durumu halâ duruyor yalnız. :(
Şöyle bir case düşünelim
function result(data){
console.log(a);
}
var a=0;
ajax("..pre-cached-url", result);
a++;
-----------------------------------------------
Will this code print 0 or 1 ? Depends... on the conditions;
(0: sync callback invocation)
(1: async callback invocation)
API nin her zaman async çalışıp çalışmayacağı bir soru işareti. O yüzden biz aşağıdaki gibi
'asyncify(...)' tarzı bir utility kullanmalıyız
-------------------------------------------------------------------------------------
function asyncify(fn){
var orig_fn = fn;
var intv = setTimeout(function(){
intv = null;
if (fn) fn();
},0);
fn = null;
return function(){
//firing too quickly, before 'intv' timer has fired to
//indicate async turn passed ?
if(intv){
fn = orig_fn.bind.apply(orig_fn, [this].concat([].slice.call('arguments')));
}
else{
//invoke original function
orig_fn.apply(this, arguments)
}
}
}
function result(data){
console.log(a);
}
var a=0;
ajax("..pre-cached-url", asyncify(result));
a++;
--------------------------------------------------------------------------------
---Evet bir issue daha halledildi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment