Skip to content

Instantly share code, notes, and snippets.

@defims
Created August 1, 2014 14:03
Show Gist options
  • Select an option

  • Save defims/152244a6001274fec3ff to your computer and use it in GitHub Desktop.

Select an option

Save defims/152244a6001274fec3ff to your computer and use it in GitHub Desktop.
curry callback
<html>
<body>
<style>
script {
display : block;
margin : 1em;
border-radius : .5em;
white-space : pre;
font-family : monospace;
line-height : 1.5em;
background : lightgray;
}
</style>
<script>
//normal:
function ajax(method ,callback ,msg) {
console.log("do something");
callback(msg);
}
ajax("get" ,function(msg) {
console.log(msg)
}, "hello");
ajax("get" ,function(msg) {
console.log(msg)
}, "world");
ajax("get" ,function(msg) {
console.log(msg)
}, "!");
ajax("post" ,function(msg) {
console.log(msg)
} ,"hello");
ajax("post" ,function(msg) {
console.log(msg)
} ,"world");
ajax("post" ,function(msg) {
console.log(msg)
} ,"!");
</script>
<script>
//bind:
function ajax(method ,callback) {
console.log("do something");
callabck();
}
var get = ajax.bind(this ,"get");
var post = ajax.bind(this ,"post");
get(function(msg) {
console.log("get:" ,msg)
} ,"hello");
get(function(msg) {
console.log("get:" ,msg)
} ,"world");
get(function(msg) {
console.log("get:" ,msg)
} ,"!");
post(function(msg) {
console.log("post:" ,msg)
} ,"hello");
post(function(msg) {
console.log("post:" ,msg)
} ,"world");
post(function(msg) {
console.log("post:" ,msg)
} ,"!");
</script>
<script>
//curry
function ajax(method ,callback ,msg){
console.log("do something");
callback(msg);
}
var _ = this;
var get = ajax.bind(_ ,"get");
var getAndPrint = get.bind(_ ,function(msg){
console.log("get:" ,msg)
})
var post = ajax.bind(_ ,"post");
var postAndPrint = post.bind(_ ,function(msg){
console.log("post:" ,msg)
})
getAndPrint("hello");
getAndPrint("world");
getAndPrint("!");
postAndPrint("hello");
postAndPrint("world");
postAndPrint("!");
</script>
<script>
function first(callback) {
console.log("first");
callback();
}
function second(callback) {
console.log("second");
callback();
}
function third(callback) {
console.log("third");
callback();
}
function forth(callback) {
console.log("forth");
callback();
}
//nest callback
first(function() {
second(function() {
third(function() {
forth(function() {
console.log("bang!")
});
});
});
});
</script>
<script>
console.log("\ncurrying way:")
function first(callback) {
console.log("first");
callback();
}
function second(callback) {
console.log("second");
callback();
}
function third(callback) {
console.log("third");
callback();
}
function forth(callback) {
console.log("forth");
callback();
}
var _ = this;
var bang = console.log.bind(console ,"bang!");
var _forth = forth.bind(_ ,bang);
var _third = third.bind(_ ,_forth);
var _second = second.bind(_ ,_third);
var _first = first.bind(_ ,_second);
_first();
</script>
<script>
function first(callback) {
console.log("first");
callback();
}
function second(callback) {
console.log("second");
callback();
}
function third(callback) {
console.log("third");
callback();
}
function forth(callback) {
console.log("forth");
callback();
}
console.log("\npromise way:")
var _first = new Promise(first);
var _second = new Promise(second);
var _third = new Promise(third);
var _forth = new Promise(forth);
_first.then(_second).then(_third).then(_forth).then(function() {
console.log("bang!")
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment