Forked from willrstern/gist:af3a3308fc5864cf48f8
Last active
November 22, 2021 17:41
-
-
Save learncodeacademy/d74d8c426921ebb16728 to your computer and use it in GitHub Desktop.
Revisions
-
willrstern revised this gist
Aug 6, 2014 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,9 +21,10 @@ Promise.all(parallelCalls).spread(function(profile, accounts) { ```javascript // with Co (no yieldHandler is required) co(function* () { var [profile, accounts] = yield [ getUrl('/api/profile'), getUrl('/api/accounts'), ]; //run both now, wait till both have resolved console.log('done', yield getUrl('/api/messages/' + profile.id)); })(function(err) { if (err) { console.log('err', err); } @@ -37,9 +38,10 @@ Promise.coroutine.addYieldHandler(function(yieldedValue) { }); Promise.coroutine(function* () { var [profile, accounts] = yield [ getUrl('/api/profile'), getUrl('/api/accounts'), ]; //our custom yield handler will handle this just like co...waiting till both have resolved console.log('done', yield getUrl('/api/messages/' + profile.id)); })().catch(function(errs) { //handle errors on any events -
willrstern revised this gist
Jul 31, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ Promise.coroutine(function* () { //handle errors on any events }) ``` Q...I don't like this way of having to do things, mixing promises feels like kluge ```javascript // with Q - hopefully somebody can correct me with a better way Q.spawn(function* () { -
willrstern revised this gist
Jul 31, 2014 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,10 +21,10 @@ Promise.all(parallelCalls).spread(function(profile, accounts) { ```javascript // with Co (no yieldHandler is required) co(function* () { var profile = getUrl('/api/profile'); var accounts = getUrl('/api/accounts'); yield [profile, accounts]; //run both now, wait till both have resolved console.log('done', yield getUrl('/api/messages/' + profile.id)); })(function(err) { if (err) { console.log('err', err); } }); @@ -33,7 +33,7 @@ co(function* () { // with bluebird //add a yieldHandler somewhere in your config, so yielded arrays are treated as .all()'s Promise.coroutine.addYieldHandler(function(yieldedValue) { if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue); }); Promise.coroutine(function* () { -
willrstern revised this gist
Jul 31, 2014 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,8 +19,19 @@ Promise.all(parallelCalls).spread(function(profile, accounts) { ##The Best Way: With Generators## ```javascript // with Co (no yieldHandler is required) co(function* () { var profile = getUrl('/api/profile'); var accounts = getUrl('/api/accounts'); yield [profile, accounts]; //run both now, wait till both have resolved console.log('done', yield getUrl('/api/messages/' + profile.id)); })(function(err) { if (err) { console.log('err', err); } }); ``` ```javascript // with bluebird //add a yieldHandler somewhere in your config, so yielded arrays are treated as .all()'s Promise.coroutine.addYieldHandler(function(yieldedValue) { if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue); }); @@ -34,17 +45,6 @@ Promise.coroutine(function* () { //handle errors on any events }) ``` Q...don't like this way of having to do things, mixing promises in the loop ```javascript // with Q - hopefully somebody can correct me with a better way -
willrstern revised this gist
Jul 31, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ##Example Task: Make 2 parallel (simultaneous) async calls, then one async call when they've resolved## ##The Good Way: Promises## ```javascript -
willrstern revised this gist
Jul 31, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ##Example Task: Make 2 async calls, then one async call when they've resolved## ##The Good Way: Promises## ```javascript -
willrstern renamed this gist
Jul 31, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
willrstern created this gist
Jul 31, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ ##Our Example: make 2 async calls, then one async call when they've resolved## ##The Good Way: Promises## ```javascript //with bluebird library var parallelCalls = [ getUrl('/api/profile'), getUrl('/api/accounts') ]; //spread is like .then(), except it apply()s the array of responses as individual arguments Promise.all(parallelCalls).spread(function(profile, accounts) { return getUrl('/api/messages/' + profile.id); }).then(function(messages) { console.log('GOT MESSAGES', messages); }, function(errs) { //handle errors }); ``` ##The Best Way: With Generators## ```javascript // with bluebird //add a yieldHandler, so yielded arrays are treated as .all()'s Promise.coroutine.addYieldHandler(function(yieldedValue) { if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue); }); Promise.coroutine(function* () { var profile = getUrl('/api/profile'); var accounts = getUrl('/api/accounts'); yield [profile, accounts]; //run both now, wait till both have resolved console.log('done', yield getUrl('/api/messages/' + profile.id)); })().catch(function(errs) { //handle errors on any events }) ``` ```javascript // with Co (no yieldHandler is required) co(function* () { var profile = getUrl('/api/profile'); var accounts = getUrl('/api/accounts'); yield [profile, accounts]; //run both now, wait till both have resolved console.log('done', yield getUrl('/api/messages/' + profile.id)); })(function(err) { if (err) { console.log('err', err); } }); ``` Q...don't like this way of having to do things, mixing promises in the loop ```javascript // with Q - hopefully somebody can correct me with a better way Q.spawn(function* () { try { var signin = getUrl('/api/profile'); var profile = getUrl('/api/accounts'); var signinAndProfile = yield Q.all([signin, profile]); //run both now, wait till both have resolved console.log('done', yield getUrl('/api/messages/' + signinAndProfile[0].id)); } catch(errs) { //handle any errors } }); ```