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

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -