0
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:

  1. Download information about a screencast hosted on YouTube using the youtube module.
  2. 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.

Community
  • 1
  • 1
Angular noob
  • 437
  • 4
  • 11
  • As the author of the comprehensive answer(s), I'd like to know: What are you missing? How could I improve the answers? Btw, I think that [nesting](http://stackoverflow.com/a/28250687/1048572) is the best solution for your case. – Bergi May 06 '15 at 17:14
  • What approach from the ones listed in the canonical question did you choose? Can you show us how you tried to apply it to your code, please? I'd be happy to reopen this question and help you with your specific problem. – Bergi May 06 '15 at 17:15

2 Answers2

1

Well, there are two ways:

  1. Create a global variable screencast (like you already have a screencastId) which is shared between all .then calls
  2. 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)
})
smnbbrv
  • 23,502
  • 9
  • 78
  • 109
  • I did not think about that! Could I please see an example of returning a new `Promise`? – Angular noob May 06 '15 at 09:59
  • added a small example of it. You always can make a function which generates a new promise with sc to avoid this big structure in every then statement – smnbbrv May 06 '15 at 10:01
1

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

Stefano Vuerich
  • 996
  • 1
  • 7
  • 28