asynchronous - Javascript: How to determine whether to promisefy a function? -


consider small helper functions, of obvious candidates async / promise (as think understand them):

exports.function processfile(file){   return new promise(resolve => {     // super long processing of file     resolve(file);   }); } 

while this:

exports.function addone(number){   return new promise(resolve => {     resolve(number + 1);   }); } 

seems overkill.

what rule 1 determines whether should promise-fy functions?

i use promise if need execute multiple async functions in order. example, if need make 3 requests , wait them come before can combine data , process them, that's great use case promises. in case, let's have file, , need wait until file , metadata service both loaded, put them in promise.

if you're looking optimize operations on long running processes, i'd more web workers using promises. operate outside ui thread , use messages pass progress ui thread. it's event based, no need promise. caveat there can't perform actions on dom inside web worker.

more on web workers here: https://developer.mozilla.org/en-us/docs/web/api/web_workers_api/using_web_workers


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - VueJS2 and the Window Object - how to use? -