Home > front end >  Running Stripe Asynchronously
Running Stripe Asynchronously

Time:09-28

The following code takes few second to run:

payments = stripe.PaymentIntent.list(limit=10000)

How I can make the above code run asynchronously?

I tried await payments = stripe.PaymentIntent.list(limit=10000) but I received the error SyntaxError: cannot assign to await expression

CodePudding user response:

You can kick it off without awaiting by calling an async function:

async function listPaymentIntents() {
  const payments = await stripe.PaymentIntent.list(limit=10000);
  console.log('done!');
}

console.log('calling listPaymentIntents!');
listPaymentIntents();
console.log('called listPaymentIntents!');

And yes as @Barmar mentions the await goes on the value side to handle the promise resolution.

CodePudding user response:

import time
import asyncio       
import threading


async def myfunction():

    await asyncio.sleep(10) #  sleep for 10 seconds

    payments = stripe.PaymentIntent.list(limit=10000)


server=threading.Thread(target=asyncio.run, args=(myfunction(),))

server.start()
  • Related