CorkGPT

Explore Models

Library

Today

What's the difference between synchronous and asynchronous functions in JavaScript?

How do I fix a CORS error in my React app?

Can you explain the difference between supervised and unsupervised learning?

Yesterday

What is a closure in JavaScript?

How can I avoid memory leaks in C++?

How can I implement multithreading in Python?

June

How do I deploy a React app to GitHub Pages or Netlify?

What is the difference between continuous integration and continuous deployment?

How do I connect my GitHub repo to a CI pipeline using GitHub Actions?

May

How do I set up environment variables securely in production?

How can I automate server updates and backups?

What is a hash table, and how does it work?

April

How do I solve the two-sum problem in JavaScript?

How do I find the longest common subsequence in two strings?

How does a binary search algorithm work?

March

How can I detect overfitting in a machine learning model?

What is a confusion matrix, and how do I interpret it?

How do I use ChatGPT with the OpenAI API?

February

What's the difference between var, let, and const in JavaScript?

What is the DOM and how does JavaScript interact with it?

What is the purpose of useEffect in React?

January

What are the differences between NGINX and Apache?

How do I monitor a Linux server's resource usage?

What are the differences between GPT and BERT?

What can I help with?

What's the difference between synchronous and asynchronous functions in JavaScript?

Great question!
  • Synchronous functions execute one after the other. Each operation waits for the previous one to finish before moving on.
  • Asynchronous functions allow other operations to continue while waiting for a task (like fetching data) to complete. This is useful for things like API calls or file reading.
Example:

javascript

// Synchronous
console.log("Start");
console.log("Middle");
console.log("End");

// Output:
// Start
// Middle
// End

// Asynchronous
console.log("Start");
setTimeout(() => {
    console.log("Middle");
}, 1000);
console.log("End");

// Output:
// Start
// End
// Middle
Copy

So setTimeout is asynchronous? What else is asynchronous in JavaScript?

Yes, exactly — setTimeout is asynchronous because it doesn't block the main thread.

Other common asynchronous features in JavaScript include:

  • fetch() API (for HTTP requests)
  • Promises
  • Async/await syntax
  • Event listeners (like onclick, onload)
  • Web APIs (like geolocation, file reading, etc.)

These allow tasks to happen in the background while your code keeps running.

Can you show me an example using async/await with fetch?

Absolutely! Here's a simple example of how to use async/await with the fetch API:

javascript

async function getUserData() {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/users/1');
        const data = await response.json();
        console.log(data);
    } catch (error) {
        console.error('Error fetching data:', error);
    }
}

getUserData();
Copy
What's happening:
  • async makes the function return a promise.
  • await pauses the function execution until the fetch completes.
  • If an error occurs (e.g., network failure), it's caught by the try...catch.

What happens if I remove await?

If you remove await, the function won't wait for the fetch to complete before moving on. You'll get a Promise object instead of the actual data.

Example:

javascript

async function getUserData() {
    const response = fetch('https://jsonplaceholder.typicode.com/users/1');
    console.log(response); // Logs: Promise {<pending>}
}
Copy

You'd need to manually handle the promise with .then() or put await back to resolve it inside the async function.