Skip to content

Instantly share code, notes, and snippets.

@CliffCrerar
Last active March 25, 2020 08:02
Show Gist options
  • Save CliffCrerar/0be223378455268409eb66cab99c05c5 to your computer and use it in GitHub Desktop.
Save CliffCrerar/0be223378455268409eb66cab99c05c5 to your computer and use it in GitHub Desktop.
Javascript async patterns
/*****************************************************************************
< How do you delay functions in JavaScript. />
3 Methods:
1. Callback
2. Promise
3. Observable
With Javascript there are always creative ways to do almost any kind of
delay, these methods are pure code and not time based.
*****************************************************************************/
// 1. Using a callback
/* DECLARATION */
function theMainFunction(...someParams /* < optional */, callback){
// some logic that runs
return callback();
}
function theDelayedFunction(){
// some logic
return;
}
/* EXECUTION */
theMainFunction(...someParams ,function(){
theDelayedFunction();
})
// Using a promise
function theSecondMainFunction(){
// RUNS FIRST
var result = 'foo';
return new Promise((resolve,reject)=>{
// RUNS SECOND
const valid = true; // < this is optional
// you can do some more logic here
if(valid){
resolve(result);
} else {
// you can even run validation in this block and if it fails
// then execute the reject
reject(); // < this is optional
}
// note that in the returned promise code block essentially you could only run the resolve(result) all the other code is not required but it is good practice.
})
}
theSecondMainFunction().then(function(result){
// RUNS THIRD
console.log(result); // logs "foo" to the console
}).catch(function(err){
// OPTIONAL CODE BLOCK
// RUNS IF REJECT IS EXECUTED INSTEAD OF RESOLVED.
}).finally(function(){
// OPTIONAL CODE BLOCK
// RUNS FOURTH REGARDLESS OF RESOLVED OR REJECTED
});
// The observable pattern
class Observable {
constructor(){
this.callbacks = []
}
subscribe( fn ) {
this.callbacks.push( fn );
}
publish( data ) {
this.callbacks.forEach( fn => fn( data ) );
}
unsubscribe() {
delete this
}
}
module.exports = Observable;
// in some other part of the application
const Observable = require('path to file with observable class');
const observable = new Observable(); // instantiate the observable
function thirdMainFunction(){
// RUNS FIRST
// Some logic that produces a result
result = 'bar';
observable.publish(result);
}
// for demo purpose this is underneath the "thirdMainFunction"
// But once the observable has been instantiated it can
// be exported with "module.exports" and imported with "require"
// anywhere in the application and subscribe would bring whatever
// to that module.
observable.subscribe(function(result){
// RUNS SECOND
console.log(result); // will log "bar" to console
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment