Skip to content

Instantly share code, notes, and snippets.

@bluepnume
Created June 5, 2017 15:22
Show Gist options
  • Save bluepnume/06769bbb783bbb71e68f255e125d83ee to your computer and use it in GitHub Desktop.
Save bluepnume/06769bbb783bbb71e68f255e125d83ee to your computer and use it in GitHub Desktop.
// Here's a mock ajax function
function ajax(url, callback) {
setTimeout(function() {
if (Math.random() < 0.25) {
callback(new Error(`Something went wrong calling ${url}`))
} else {
callback(null, {
url: url,
data: {
foo: 'bar'
}
})
}
}, 1000);
}
// And here's how you call it:
ajax('/api/xxx', function(err, result) {
if (err) {
console.error('We got an error:', err);
} else {
console.info('We got a result:', result);
}
});
// 1. Call /api/user/danny, /api/user/jilly, and /api/user/partario one after another, then console log each user after ALL the calls have completed
// 2. Call /api/user/danny, /api/user/jilly, and /api/user/partario in parallel, then console log each user after ALL the calls have completed
// 3. Use async.js to make the three calls in parallel
@wildlingjill
Copy link

// Here's a mock ajax function

function ajax(url, callback) {
  setTimeout(function() {
    if (Math.random() < 0.25) {
      callback(new Error(`Something went wrong calling ${url}`))
    } else {
      callback(null, {
        url: url,
        data: {
          foo: 'bar'
        } 
      })
    }
  }, 1000);
}

// And here's how you call it:

ajax('/api/xxx', function(err, result) {
    if (err) {
        console.error('We got an error:', err);
    } else {
       console.info('We got a result:', result);
    }
});

// 1. Call /api/user/danny, /api/user/jilly, and /api/user/partario one after another, then console log each user after ALL the calls have completed
// 2. Call /api/user/danny, /api/user/jilly, and /api/user/partario in parallel, then console log each user after ALL the calls have completed
// 3. Use async.js to make the three calls in parallel


// 1.

ajax('/api/user/danny', function(err, result){
  if (err){
    console.warn("Error: ", err);
  } else {
    ajax('/api/user/jilly', function(err, result2){
      if (err) {
        console.warn("Error 2: ", err);
      } else {
        ajax('/api/user/partario', function(err, result3){
          if (err){
            console.warn("Error 3: ", err);
          } else {
            console.info(["Result 1: ", result], ["Result 2: ", result2], ["Result 3: ", result3]);
          }
        })
      }
    })
  }
})

// 2.

function callAjax (callback) {
  var errors = false;
  var responses = [];
  var counter = 3;
  
  ajax('/api/user/danny', function(err, response){
    if(errors === false){
      if(err){
        callback(err, null);
        errors = true;
      } else {
        responses.push(response);
        counter--;
        if (counter === 0){
          callback(null, responses);
        }
      }
    }
  });
  
  ajax('/api/user/jilly', function(err, response){
    if (errors === false){
      if(err){
        callback(err, null);
        errors = true;
      } else {
        responses.push(response);
        counter--;
        if (counter === 0){
          callback(null, responses);
        }
      }
    }
  });
  
  ajax('/api/user/partario', function(err, response){
    if (errors === false){
      if(err){
        callback(err, null);
        errors = true;
      } else {
        responses.push(response);
        counter--;
        if (counter === 0){
          callback(null, responses);
        }
      }
    }
  });
}

callAjax(function(error, responses){
  if (error !== null){
    console.warn(error);
    return error;
  } else if (responses !== null) {
    for (var i = 0; i < responses.length; i++){
      console.info(responses[i]);
    }
  }
})

// 3.

async.parallel([
  function(callback){
    ajax('/api/user/danny', function(err, response){
      callback(err, response);
    })
  },
  function(callback){
    ajax('/api/user/jilly', function(err, response){
      callback(err, response);
    })
  },
  function(callback){
    ajax('/api/user/partario', callback)
  },
],

  function(err, results){
    if(err){
      console.warn(err);
      return err;
    } else {
      console.info(results);
    }
  }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment