asynchronous - Abstracting away asynchronicity/promises in javascript? -


suppose have written library class:

class complexthingdoer implements thingdoer {   ...   docomplexthing(arg1, arg2) {      // lots of complex code here      return finalresult;   }   ... } 

and have made class available others, using intended, ignorant of how docomplexthing complex thing , making use of finalresult whatever they're using for.

now, suppose decide want reimplement docomplexthing instead of synchronously running lot of complex code here, makes request specialized server returns final result. or makes call database in results of complex thing have been precalculated. or 1 of things first time it's called particular set of arguments, , caches result next time it's called same arguments can return previously-calculated value.

as far can tell, whether use callbacks, promises, or async/await, screwed - can't of things without breaking interface. have return promise, or accept callback, or - there doesn't seem way not return until have result, way i've done before. callers don't care how i'm getting result; want result.

am missing something? there in fact way "de-promisify" function caller doesn't have know it's performing asynchronous operation? should pre-emptively writing promises in case someday might want reimplement in asynchronous way (sounds terrible idea)?

thanks!

nope, you're not missing anything. if make function asynchronous, every caller of have treat such , asynchronous itself. result of asynchronous call available sometime later, on subsequent iterations of underlying event loop. there absolutely no way block synchronous code wait that, because if block in code event loop won't advance , result never become available.

switching interface synchronous asynchronous breaking change.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -