Skip to content

Instantly share code, notes, and snippets.

@learncodeacademy
Forked from willrstern/gist:af3a3308fc5864cf48f8
Last active November 22, 2021 17:41
Show Gist options
  • Select an option

  • Save learncodeacademy/d74d8c426921ebb16728 to your computer and use it in GitHub Desktop.

Select an option

Save learncodeacademy/d74d8c426921ebb16728 to your computer and use it in GitHub Desktop.

Revisions

  1. @willrstern willrstern revised this gist Aug 6, 2014. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions gistfile1.md
    Original 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 = getUrl('/api/profile');
    var accounts = getUrl('/api/accounts');
    yield [profile, accounts]; //run both now, wait till both have resolved
    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 = getUrl('/api/profile');
    var accounts = getUrl('/api/accounts');
    yield [profile, accounts]; //run both now, wait till both have resolved
    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
  2. @willrstern willrstern revised this gist Jul 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ Promise.coroutine(function* () {
    //handle errors on any events
    })
    ```
    Q...don't like this way of having to do things, mixing promises in the loop
    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* () {
  3. @willrstern willrstern revised this gist Jul 31, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.md
    Original 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));
    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);
    if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue);
    });

    Promise.coroutine(function* () {
  4. @willrstern willrstern revised this gist Jul 31, 2014. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions gistfile1.md
    Original 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, so yielded arrays are treated as .all()'s
    //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
    })
    ```
    ```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
  5. @willrstern willrstern revised this gist Jul 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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##
    ##Example Task: Make 2 parallel (simultaneous) async calls, then one async call when they've resolved##

    ##The Good Way: Promises##
    ```javascript
  6. @willrstern willrstern revised this gist Jul 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ##Our Example: make 2 async calls, then one async call when they've resolved##
    ##Example Task: Make 2 async calls, then one async call when they've resolved##

    ##The Good Way: Promises##
    ```javascript
  7. @willrstern willrstern renamed this gist Jul 31, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @willrstern willrstern created this gist Jul 31, 2014.
    61 changes: 61 additions & 0 deletions gistfile1.txt
    Original 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
    }
    });
    ```