Passing values from one promise to the next

Viewed 1581

A bit new to javascript. Been dealing with promises, however ran into a problem i dont know how to approach.

How can i pass a value into the next promise resolve?

Here's my code

bot.on('ask.add_account_password', (msg) => {
    let username = users[msg.from.id].username;
    let password = msg.text;
    var accounts = require('./inc/account.js');
    accounts.login_account(username,password).then(function(data){
        var account = data.params;
        console.log(account);
        return accounts.store(msg.from.id,username,password,account.followerCount);
    }).then(function(data){
        let caption = "Your account "+account.username+"("+account.fullName+")has been added\n";
        return bot.sendPhoto(msg.from.id, account.picture, {caption:caption});
    })
    .catch(function(error){
        console.log(error);
        add_account(msg,error.name);
    });
});

On the line where i create the caption variable, i'm trying to access the account object created in the block before it(var account = data.params) but i get a reference error saying its not defined. Now i can easily bypass this by just sending the entire object into the accounts.store function and have it resolve the object when done, but that just seems like a dirty workaround to a bigger problem. Is there a cleaner way to do this?

3 Answers
Related