Skip to content

Exploring Python and JavaScript Comparisons for Async Functions Making HTTP Calls

Notifications You must be signed in to change notification settings

dfrho/async-python-and-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

Asynchronous Functions and HTTPS Calls: JavaScript vs. Python

This document compares asynchronous functions and HTTPS calls in JavaScript and Python, focusing on syntax and implementation for both task types.

1. Asynchronous Functions

JavaScript

JavaScript uses the async/await syntax for handling asynchronous operations, built on top of Promises.

// Asynchronous function using async/await
async function fetchData() {
    try {
        const response = await fetch('https://api.example.com/data');
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        const data = await response.json();
        console.log(data);
    } catch (error) {
        console.error('Fetch error:', error);
    }
}

// Call the async function
fetchData();

#### Key Points:
- **`async` Keyword**: Declares an asynchronous function.
- **`await` Keyword**: Pauses execution until the promise resolves.
- **Error Handling**: Implemented using `try/catch` blocks.

### Python

Python employs the `asyncio` library along with the `async`/`await` syntax to manage asynchronous operations.

```python
import asyncio

# Asynchronous function
async def fetch_data():
    print('Fetching data...')
    await asyncio.sleep(2)  # Simulate an asynchronous operation (e.g., network call)
    print('Data fetched')

# Run the asynchronous function
asyncio.run(fetch_data())

Key Points:

  • async def: Defines an asynchronous function.
  • await: Pauses execution until the awaited task completes.
  • asyncio.run(): Executes the asynchronous function in an event loop.

2. HTTP Calls

JavaScript

JavaScript uses the fetch API for making HTTP calls, which return promises for handling asynchronous requests.

GET Request

// GET request using fetch
fetch('https://api.example.com/data')
    .then(response => {
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        return response.json();
    })
    .then(data => {
        console.log(data);
    })
    .catch(error => {
        console.error('Fetch error:', error);
    });

POST Request

// POST request using fetch
fetch('https://api.example.com/data', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({ key: 'value' })
})
    .then(response => {
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        return response.json();
    })
    .then(data => {
        console.log(data);
    })
    .catch(error => {
        console.error('Fetch error:', error);
    });

Python

Python supports both synchronous and asynchronous HTTP calls using different libraries.

Asynchronous HTTP Calls with aiohttp

GET Request
import aiohttp
import asyncio

# Asynchronous function to make an HTTP GET request
async def fetch_data():
    async with aiohttp.ClientSession() as session:
        async with session.get('https://api.example.com/data') as response:
            if response.status != 200:
                raise Exception('Network response was not ok')
            data = await response.json()
            print(data)

# Run the asynchronous function
asyncio.run(fetch_data())
POST Request
import aiohttp
import asyncio

# Asynchronous function to make an HTTP POST request
async def post_data():
    async with aiohttp.ClientSession() as session:
        async with session.post('https://api.example.com/data', json={'key': 'value'}) as response:
            if response.status != 200:
                raise Exception('Network response was not ok')
            data = await response.json()
            print(data)

# Run the asynchronous function
asyncio.run(post_data())

Synchronous HTTP Calls with requests

GET Request
import requests

# Synchronous GET request
response = requests.get('https://api.example.com/data')
if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print('Network response was not ok')
POST Request
import requests

# Synchronous POST request
response = requests.post('https://api.example.com/data', json={'key': 'value'})
if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print('Network response was not ok')

Summary

  • JavaScript:

    • Asynchronous Functions: Utilize the async/await syntax and promises.
    • HTTP Calls: Use the fetch API for both GET and POST requests.
  • Python:

    • Asynchronous Functions: Employ asyncio with async/await syntax.
    • HTTP Calls:
      • Asynchronous: Use aiohttp for non-blocking operations.
      • Synchronous: Use requests for straightforward HTTP requests.

Both languages provide powerful tools for asynchronous programming and HTTP interactions, each with unique syntax and implementation details.

I wondered about the efficiency, still thinking Python is more concise. The percentage difference in the number of lines between JavaScript and Python for these tasks is approximately -4.44%, indicating that the Python examples have slightly more lines overall, although a negligible difference.

This shows that both languages have similar levels of verbosity for the tasks demonstrated, with Python being slightly more concise in some cases and JavaScript in others.

About

Exploring Python and JavaScript Comparisons for Async Functions Making HTTP Calls

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published