Back

Inserting delay in appwrite function.

  • 0
  • Databases
  • Functions
  • Web
sivagowda
14 Jun, 2023, 13:52

I have a appwrite function which make several(depending on situation, some where between 100 and 500) api calls to appwrite database(database.updateDocument). I want to insert a delay of 200ms between each update api call( becouse I am listening to database changes via real time channels. I want to insert delay becouse if updates are spontaneous then I can not listen to that changes and updates my UI at the frontend). I tried using setTimeOut. But it is not working as expected. How can I insert delay?

TL;DR
The user was having issues with a React Native app where they were subscribed to a channel and received an error. They resolved it by modifying a line of code in a file called `sdk.js`. They then asked about adding a delay between API calls. Another user suggested using the `sleep` function with `setTimeout` in JavaScript to achieve the desired delay. The user confirmed that this solution worked for them.
Binyamin
14 Jun, 2023, 13:53

Is this JavaScript?

sivagowda
14 Jun, 2023, 13:53

yes

Binyamin
14 Jun, 2023, 13:55

Try something like this, First create this function

TypeScript
  const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

Then you can use it each time.

TypeScript
while(true){
   await sleep(200);
   // ... your code
}
sivagowda
14 Jun, 2023, 13:59

I tried this. But it didn't work. The function is executed with in 200ms, even though my delay is 100ms between each api call.

sivagowda
14 Jun, 2023, 14:04

😟

Binyamin
14 Jun, 2023, 14:18

That's weird Can you try to add

TypeScript
console.log(+new Date());

inside the loop?

Binyamin
14 Jun, 2023, 14:18

To see if there's any delays?

sivagowda
14 Jun, 2023, 17:24

some how now its working. thank you

sivagowda
14 Jun, 2023, 17:29

I am using react-native for developing an app. I got an error when I tried to subscribe to a channel. I resolved this by modifying file appwrite\dist\cjs\sdk.js line 187 as const cookie = JSON.parse((_a = typeof window.localStorage !== 'undefined' && window.localStorage.getItem('cookieFallback')) !== null && _a !== void 0 ? _a : '{}'); thank you

Drake
14 Jun, 2023, 18:19

try searching <#1072905050399191082>. if you're still stuck, it's best to create a new post since this is a different topic.

sivagowda
14 Jun, 2023, 18:55

All issues got resolved. Thank you

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more