Sarah Muenzinger
SarahMunzi
Javascript allows for asynchronous calls
that work something like this
console.log("First");
TweetDatabase.getTweets(function(err, tweets){
console.log("Third!");
console.log(tweets);
});
console.log("Second!");
TweetDatabase.getTweet(function(err, tweet){
tweet.getAllReTweets(function(err, retweets){
retweets.getUsers(function(err, users){
users.followAll();
})
})
})
try {
TweetDatabase.getTweet(function(err, tweet){
tweet.getAllReTweets(function(err, retweets){
retweets.getUsers(function(err, users){
users.followAll();
})
})
})
}
catch(err) {
console.log("nested errors won't bubble! :(")
}
TweetDatabase.getTweet(function(err, tweet){
if(err){
console.log("we found an err", err)
}
tweet.getAllReTweets(function(err, retweets){
if(err){
console.log("we found an err", err)
}
retweets.getUsers(function(err, users){
if(err){
console.log("we found an err", err)
}
users.followAll();
})
})
})
//somewhere earlier in the code
var users = retweets.getUsers();
//somewhere later in the code,
// maybe even in another file
users.followAll();
//somewhere earlier in the code
var promisedUsers = retweets.getUsers().then()
...
promisedUsers.then(function(users){
users.followAll();
})
TweetDatabase.getTweet()
.then(function(tweets){
return tweet.getAllReTweets();
})
.then(function(retweets){
return retweets.getUsers();
})
.then(function(users){
users.followAll();
})
.catch(function(err){
console.log("we found an err", err)
})
TweetDatabase.getTweet()
.then(function(tweets){
tweet.getAllReTweets()
.then(function(retweets){
retweets.getUsers()
.then(function(users){
users.followAll();
})
})
})
.catch(function(err){
console.log("we found an err", err)
})
Sarah Muenzinger
sarah.muenzinger@gmail.com
SarahMunzi