Aug 24, 2017 08:56 PM
(This post was last modified: Aug 25, 2017 02:38 PM by uint32.)
They are three main asynchronous programming patterns:
TAP: commonly used by the form of promises or observables in javascript. Returns an object that can be queued for results. The new ES6 async/await pattern. Example:
EAP: event is set and fired on completion. Example:
CAP: callback function is passed as an argument. Example:
So, which is your personal favorite? I've heard a lot of debate over this topic, and nobody seems decided on which is best.
Personally, I prefer a TAP approach because I find it easier to understand, maintain and clean.
- TAP (task-based asynchronous pattern)
- EAP (event-based asynchronous pattern)
- CAP (callback-based asynchronous pattern)
TAP: commonly used by the form of promises or observables in javascript. Returns an object that can be queued for results. The new ES6 async/await pattern. Example:
Code
function getUser(){
return new Promise((resolve, reject) => { ...some asynchronous stuff... resolve(result) }
}
let user = await getUser();EAP: event is set and fired on completion. Example:
Code
let emitter = new EventEmitter();
function getUser(){
...some asynchronous stuff...
emitter.emit("something", params)
}
emitter.on("something", (params) => { ...whatever... });
getUser();CAP: callback function is passed as an argument. Example:
Code
function getUser(callback){
...some asynchronous stuff...
callback(params)
}
getUser((params) => { ...whatever... });So, which is your personal favorite? I've heard a lot of debate over this topic, and nobody seems decided on which is best.
Personally, I prefer a TAP approach because I find it easier to understand, maintain and clean.




