Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

Favorite asynchronous pattern

They are three main asynchronous programming patterns:
  • 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.
For JS, using promises is dope, but I've noticed when using Firebase that I can't use listeners (EAP) and promises together (the promise can only be resolved once), but I can use listeners with callbacks repeatedly.

Idk if that answered your question tho lol I guess I like TAP. It depends on the use case. I use promises for getting data once, then use callbacks and listeners for getting data on changes like I said.
I hate everyone on this website.
Promises are nice indeed, but I like action based systems like redux aswell
工 ᗪㄖ𝓝丶七 丂🝗🝗 ⼕ㄖ㇄ㄖ尺⸝ チㄩセㄩ尺🝗 ⻏㇄闩⼕长 PINK!
+1 for TAP with async/await; makes life so much easier!
I find myself doing everything web-related with EAP, but generally when not doing anything specifically for the web I default back to TAP.
Dunno, TAP seems to be my goto for like every language too haha.
humble rubyist in a world of pythonistas
I like all three, though commonly been TAP. All three methods have their applications and uses. But syntax wise and preference would still be TAP.
- Valeas quam optime. -


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by morphine1337Jan 26, 2017 12:35 PM 4 174 Jan 26, 2017 12:35 PM
Last Post: morphine1337
[Javascript]  by SnittJun 12, 2016 05:55 PM 1 126 Jun 12, 2016 05:55 PM
Last Post: money.team
[Javascript]  by ~DecJan 9, 2015 11:06 PM 5 181 Jan 9, 2015 11:06 PM
Last Post: iGnite