Giter Club home page Giter Club logo

Comments (3)

dougwilson avatar dougwilson commented on August 29, 2024

Hi @davidbdeath sorry you are having trouble. That is a general Node.js / JavaScript question, unrelated to this module. You can read more about this in the Node.js guide https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/

from mysql.

davidbdyer avatar davidbdyer commented on August 29, 2024

Hi @dougwilson thanks for the quick response. I'm a little bit of a noob. I'm confused why it will output to console.log and push onto the variable locally inside the function but doesn't push onto it globally even though it's the same variable. I read the link but it's still a bit fuzzy. Do i need to create an async function? If you're too busy to explain I wont keep asking. My prof is good at mysql but not so much with javascript.

from mysql.

dougwilson avatar dougwilson commented on August 29, 2024

Hi @davidbdeath no problem. The issue is that with non-blocking operations that are common in Node.js (typically styled as a "callback"), the code does not run linear top-to-bottom -- the code inside the callback will run at a later time. So thus in the example like

console.log(1)
conn.query('SELECT 1', function () { // this is a callback
  console.log(2)
})
console.log(3)

The output will be 1, then 3, then 2. So if you declare a variable at 1, then manipulate it at 2, at 3, you won't see the value, since 3 ran before you did the manipulation, even though reading the file top-to-bottom looks like it may happen before.

from mysql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.