Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:08:49+00:00 2026-05-23T07:08:49+00:00

I am making a chrome extension which sets a cookie when users log in.

  • 0

I am making a chrome extension which sets a cookie when users log in. When I attempt to read the cookie using the chrome.cookies.get() method the callback can log the results but I cant pass it out of the callback.

function getCookie (cookieName){
    var returnVal; 
    chrome.cookies.get({
        'url':'https://addictedtogether.com/',
        'name':cookieName
    },
    function(data){
        console.log(data); //log displays returned cookie in a object
        returnVal=data;
    }
    );
    console.log(returnVal);  //log says this is undefined
    return returnVal;
}

I tried using a couple different ways of passing the result but it seems like the object is undefined unless it is called from within the callback.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:08:50+00:00Added an answer on May 23, 2026 at 7:08 am

    The problem is that your callback is called after the main function returns. (The extension APIs are called asynchronous for a reason!) returnVal is undefined because it hasn’t been assigned to yet. Try modifying your function to accept a callback argument:

    function getCookie (cookieName, callback){
        chrome.cookies.get({
            'url':'https://addictedtogether.com/',
            'name':cookieName
        },
        function(data){
            callback(data);
        });
    }
    
    // Use like this:
    getCookie("CookieName", function(cookieData){
      // Do something with cookieData
    });
    

    If you don’t like passing callbacks around, you could also modify your function to return a deferred. If you have to handle a lot of asynchronous function calls, deferreds make your life a lot easier. Here’s an example using jQuery.Deferred:

    function getCookie (cookieName){
        var defer = new jQuery.Deferred();
        chrome.cookies.get({
            'url':'https://addictedtogether.com/',
            'name':cookieName
        },
        function(data){
            defer.resolve(data);
        });
        return defer.promise();
    }
    // Example use:
    getCookie("FooBar").done(function(data){
      // Do something with data
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a google chrome extension and trying to get reference of a local
I'm making a google chrome extension, and I need to get the current page
I'm making a chrome extension that requires fetching an xml file from a secure
I started making this simple google chrome extension in javascript. And in the beginning
I am making a Google Chrome extension and I would like to have my
I'm making a chrome extension that uses pageAction. I can set when it shows
I am making a Chrome extension and I need to increase the max width
I'm making a Chrome Extension and need to view the HTML/CSS/JS of the popup.html
I'm trying to make a google chrome extension using javascript/html. Currently, I'm simply trying
I'm making some GET requests to an App Engine app, testing in Chrome. Whilst

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.