var screencastId = 'abc'
var a = youtube.get(screencastId);
a.then(function(screencast) {
// Yay. I have the screencast information here.
console.log(screencast);
});
// How do I access the `screencast` variable down here?
connection.beginTransactionAsync().then(function() {
return connection.queryAsync('INSERT IGNORE INTO Channels SET ?', screencast.channel);
}).then(function() {
return connection.queryAsync('INSERT INTO Screencasts SET ?', screencast);
}).then(function() {
var values = tags.map(function(tag) { return [tag]; });
return connection.queryAsync('INSERT IGNORE INTO Tags VALUES ?', [values])
}).then(function() {
var values = tags.map(function(tag) { return [screencast.screencastId, tag]; });
return connection.queryAsync('INSERT INTO ScreencastTags VALUES ?', [values])
}).then(function() {
return connection.commit();
}).error(function(e) {
connection.rollback();
winston.error(e);
});
This code can be broken down into two discrete steps:
- Download information about a
screencast
hosted on YouTube using theyoutube
module. - Store information about that
screencast
in the database.
Both of the steps are asynchronous, naturally.
My question is, how do I access the screencast
argument in the second step?
I found this comprehensive answer, but as someone relatively inexperienced with JS, I cannot work out how to apply that answer here.