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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:23:28+00:00 2026-06-09T05:23:28+00:00

But for some reason, when i put variable data into naughty , it does

  • 0

But for some reason, when i put variable data into naughty, it does not have content of data.

function cookie() {
    var naughty = 'aaa';
    $.post('/cookie', function(data) {
        naughty = data;
    });
    alert(naughty); // sends "aaa"
}

Why?

UPDATE: thank you! now i got it, its because $.post() is async, so alert(naughty) will be executed before it will be filled with data.

Now i have sub-question: when i turn off async, will only function cookie() postpone execution or whole browser engine will freeze until AJAX is done?

UPDATE 2: Yes, @Kevin B, you are right, naughty is never filled… i did mistake in first code sample. I am lucky that all answers are valid anyway with only second code sample 🙂

As @Kevin B pointed out, async=false will be depreciated in jQuery 1.8. And it freezes browser until request is done…

sub-question #2: i have more code inside cookie(), after this AJAX request. So, can i somehow pause execution of cookie() untill AJAX is done, without freezing whole browser with async=false? Or do i have to find new way (using complete/success callback) of coding this idea?

LAST EDIT:
I realized that what i want is ASYNC:FALSE… So i simply used it. BTW. ASYNC:FALSE will NOT be removed from jQuery! Only special kind of use-case will be…

  • 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-09T05:23:30+00:00Added an answer on June 9, 2026 at 5:23 am

    It’s a race condition. As .post() is an asynchronous function, the alert executes before naughty gets reset to data.

    If you want the response to come back before continuing, you can use .ajax and set async to false.

    How can I stop $.post in jQuery working asynchronously?

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

Sidebar

Related Questions

This works but for some reason the comma is missing. data gets inserted as
I have a following program but for some reason it is throwing an error
I don't know why but for some reason I am not able to refer
Im trying to get this working but for some reason it's just not right.
Kick me if I'm being silly but some some reason I'm having a heck
This is probably something really simple but for some reason I just can't seem
I declared a global shift operator but for some reason the compiler cannot deduct
This should'nt be that difficult but for some reason i'm having trouble with this.
I understand this is an easy question but for some reason this just isn't
I know PHP 5 already supports SQLite but for some reason I can't get

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.