Home Passing variables through multiple callback functions with chrome.storage.sync
Reply: 0

Passing variables through multiple callback functions with chrome.storage.sync

user1097
1#
user1097 Published in May 22, 2018, 11:10 pm

I am working on a chrome extension that uses multiple callback functions. When pulling something from the chrome.storage.sync, I often use that information in another lookup which requires another callback function.

The below code is what I'm testing with to learn nested callbacks

function last(callback) {
  console.log('last is done');
  callback();
}

function middle(callback, firstVar) {
  console.log('middle is done'); alert(firstVar);
  callback()
}

function first(callback) {
  console.log('first is done');
  var firstVar = 'my first var'
  callback(firstVar);
}

function runner(callback) {
    first(function() {
        middle(function() {
            last(callback);
        });
    });
}

runner(function(){console.log('finished')});

I'm trying to pass firstVar from the first function into the middle function. I have learned how to pass variables into a callback >here< but it doesn't seem to work with nested callbacks.

I'm having a hard time wrapping my head around this. Thanks in advance for the help.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30262 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO