Fetch and loop - maximum 3 times?

HEllo, I last night tested fetch, but for the code:

async def post_data():
response = await fetch(‘https://www.wptubeposter.com/api’,{‘method’:'POST’})
json = await response.json()
console.log(‘json’, json)
return json
i=1
while i<8:
await post_data()
i+=i

It works only 3 times and stop ? allways run 3 times :slight_smile:
Second, I tried to add python pause, time.sleep(3)
But not works, how to put fetch in loop and to add some delay ?
I added on server delay, 2 seconds before set data
thanks

Whilst I don’t know why it might run three times and only three times, the short story is: we are actively working on the IO story for pyscript. We want to do it right, and it will take us some time to get around the technical hurdles of a blocking API on async calls, and interaction with the browser event loop.

2 Likes

Your fetch POST call parameters are wrong plus normally a POST requires an HTTP body.

Example posting JSON to API:
Note: I did not look up the API to review what parameters are required.

from js import fetch, Object
from pyodide import to_js
url = 'https://www.wptubeposter.com/api'
body = {'msg': 'hello world'}
headers = {
        "Content-type": "application/json"
}
response = await fetch(
    url, 
    method='POST', 
    body=json.dumps(body),
    headers=Object.fromEntries(to_js(headers)))
2 Likes