Skip to content

Instantly share code, notes, and snippets.

(cb=>{
//setTimeout() wraps the callback cb and makes it run in
//another execution context, or "thread"
//the term thread is not accurate but may provide some ease of understanding
//the different contexts that help to achieve asynchrony
setTimeout(()=>{
cb()
},0);
console.log("B")
})(()=>{
@datrine
datrine / async_callback.js
Last active September 22, 2019 13:02
async_callback
(cb=>{
//setTimeout() wraps the callback cb and makes it run in
//another execution context, or "thread"
//the term thread is not accurate but may provide some ease of understanding
//the different contexts that help to achieve asynchrony
setTimeout(()=>{
cb()
},0);
console.log("B")
})(()=>{
@datrine
datrine / synchronous_callback.js
Last active September 22, 2019 13:03
synchronous_callback
(cb=>{ //the code runs as
cb();
console.log("B")
})(()=>{
console.log("C")
});
console.log("A")
@datrine
datrine / linear_no_callback.js
Last active September 22, 2019 13:09
linear_no_callback
((a)=>{ //"linearized" code without callbacks, an async-esque way to "linearize" asynchronous code
let value=++a
value*=value
value=++value
console.log(value);
})(1); //note the semicolon
//example below might seem far-fetched/over-the-top but it does prove that linear
//code may be much easier to read than code with callback
((a)=>{
@datrine
datrine / callback_hell.js
Last active September 22, 2019 13:14
callback_hell
((a,cb)=>{
++a;
cb(a)
})/*entry point>>>>*/(1,(b)=>{
b*=b;
((c,cb)=>{
cb(c)
})(b,(d)=>{
++d;
((e,cb)=>{
@datrine
datrine / .js
Created September 22, 2019 07:53
((a,cb)=>{
++a;
cb(a)
})(1,(b)=>{
b*=b;
((c,cb)=>{
cb(c)
})(b,(d)=>{
++d;
((e,cb)=>{