javascript - Flatter calls using promises: avoiding callbackception -


this question has answer here:

i have found following use case while working promises. writing coffeescript concision reading javascript developers should straight forwards

getusername().then (username) ->   getrelatedcompany(username).then (relatedcompany) ->     registerconnexion(username, relatedcompany) 

in above request depend of above results of previous ones. what's proper way solve this:

getusername().then (username) ->   getrelatedcompany(username) .then (relatedcompany) ->   # in example, username undefined here there's less callbackception   registerconnexion(username, relatedcompany)  

edit: using bluebird promise library.

you can use promises proxies represent values:

username = getusername() company = username.then(getrelatedcompany) // assuming promise lib, otherwise shim .spread of nest once connexion = promise.all([username, company]).spread(registerconnexion)  

in bluebird, simpler , becomes:

username = getusername() company = username.then(getrelatedcompany) connexion = promise.join(username, company, registerconnexion); 

since .join designed use case in mind.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -