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 8116793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:56:35+00:00 2026-06-06T03:56:35+00:00

My theory might be a bit weak asking this, so feel free to let

  • 0

My theory might be a bit weak asking this, so feel free to let me know.

This code is part of a browser plugin using Kango framework. Basically I have code that generates a random string that’s a userID. I also have another function that sets up a big ole JSON. It looks something like this:

function createJSON() {
data = {};
data["userID"] = generateID();
}

function generateID() {
kango.invokeAsync('kango.storage.getItem', 'userID', function(data) {
    uid = data;

    if (!uid) {
      console.log("uid doesn't exist, creating uid!");
      kango.invokeAsync('kango.storage.setItem', userID, function(data) { return data; });
    } else {
      console.log("uid found!: " + uid);
      return uid;
    }

    return data;
  });
}

kango.invokeAsync docs are here. My understanding is it works a bit like an async ajax call and in that sense it doesn’t return just in time for when I’m trying to assign its value to data[“userID”].

I think I can solve this by doing doing the userID call first and then using it’s callback(a bit vague on the terminology here) to go ahead and add to the data array.

I have no clue if that’s the right way to do it though. How do I get a return of an async function to work with?

  • 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-06-06T03:56:38+00:00Added an answer on June 6, 2026 at 3:56 am

    You can’t do it like this because of the async nature of generateID(). It will return BEFORE the async call has finished, thus the data is not available:

    data["userID"] = generateID();
    

    Instead, you have to use a callback like this:

    generateID(function(val) {data["userID"] = val;});
    
    function generateID(callback) {
    kango.invokeAsync('kango.storage.getItem', 'userID', function(data) {
        uid = data;
    
        if (!uid) {
          console.log("uid doesn't exist, creating uid!");
          kango.invokeAsync('kango.storage.setItem', userID, function(data) { callback(data); });
        } else {
          console.log("uid found!: " + uid);
          callback(uid);
        }
    
      });
    }
    

    When using asynchronous functions, you CANNOT use normal sequential programming. Instead, you have to continue your execution flow in the completion callback because ONLY then is the result of the async function known.

    Thus, you won’t be able to call createJSON() and return the desired value from that. If any code after createJSON() needs the results, it also has to be in a callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In theory, this code should set the new lock status to the users and
So this might be a bit tricky to demonstrate, since the problem only appears
In theory this seems easy but I just can't get my head around this...
This might be a dumb question, but I don't get it: I have a
This is definitely a bit of a noob question, but my searches so afar
This will be a bit abstract but I have a method like this: private
I have quite a bit of experience using the basic comm and group MPI2
This question is not about any error I'm currently seeing, it's more about theory
If I write test code to test my code, the test code might have
SO this is a bit of an odd request but hoping someone on here

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.