Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Translating a specific Promise from Javascript to Python

I am trying to translate this piece of code from Javascript to Python but could not find an equivalent of a Promise and an async function in Python.

async function sendRequest(options) {
    return new Promise(resolve => {
        request(options, function(error, response, body) {
            if (error) throw new Error(error)
            resolve({
                response: response,
                body: body
            })
        })
    })
} 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are looking for python's asyncio. The high-level equivalent of a javascript Promise is an awaitable. Since you want to return an awaitable from a function, you should use the async keyword to return a coroutine, which is a type of awaitable.

As for making an async http request, I would recommend aiohttp.

It sounds like this might be some sort of assignment, so I'm not going to fill in all the blanks for you, but the aiohttp link I provided has plenty of good examples and comprehensive docs that you can dig into. The below is a template for you to work from:

import aiohttp
import asyncio

# this function returns a coroutine, which must be awaited
# elsewhere. you can use return statements normally within its
# body, but be aware that those will be returned from the
# coroutine once it has been awaited, not directly from
# send_request
async def send_request(options):
    # do stuff with aiohttp...

# this is how to call from synchronous code
loop = asyncio.get_event_loop()
loop.run_until_complete(send_request())

# this is how to call from within another async function
async def foo():
    await send_request(options)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!