Q: How do I access promise result later on? 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 the youtube 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.

A: Well, there are two ways:


*

*Create a global variable screencast (like you already have a screencastId) which is shared between all .then calls

*If for some reason you cannot use a global variable there you also can return in every .then statement a new Promise where in resolve function you pass a parameter screencast
So this is the first way how I see it (but unfortunately cannot test):
var screencastId = 'abc', screencast

youtube.get(screencastId)
.then(function(sc) {
  screencast = sc;
})

// you need this to keep the chain
.then(function(){
  return 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);
});

Returning the new Promise example:
youtube.get(screencastId)
.then(function(sc) {
  return new Promise(function(resolve,reject){
    resolve(sc)
  })
})
.then(function(sc){
  console.log(sc)
})


A: In my opinion you can have a glovar variable globalScreencast and in your first step do something like that :
a.then(function(screencast) {
    // Yay. I have the screencast information here.
    console.log(screencast);
    globalScreencast = screencast;
});

so then in your second step you can have access to the variable
