- Expo React Native SDK Version: 46
- Platforms: Android/iOS
- Package concerned : Expo.Updates
Hello everyone, I want to programmatically check for new updates, without using the fallbackToCacheTimeout in app.json that will trigger the check of the new updates when the application is launched because like that I can't put a custom loading page. So by doing this all by code as follow :
try{
const update = await Updates.checkForUpdateAsync();
if(update.isAvailable){
await Updates.fetchUpdateAsync();
await Updates.reloadAsync();
}else{}
}catch(err){}
But I want to be able to abort all those calls after a certain time (thus, the user that have a bad connection can use the app without waiting a very long time). I check the documentation and I cannot found any method that allow this.
I dont't think it's possible to cancel a Promise for now in Javascript, or maybe any connection ?
Or does the "fallbackToCacheTimeout" value in the app.json will automatically apply to the fetch updates call of the Expo API?
Do someone have any idea how to do it ? :(
First of all I am assuming you have set
updates.checkautomaticallyfield toON_ERROR_RECOVERYinapp.jsonorapp.config.jsfile. If not, please check the documentation. The reason why you need this is to avoid automatic updates which can also block your app on splash screen.Updated Solution
Because of the limitation in javascript we can't cancel any external Promise (not created by us or when its reject method is not exposed to us). Also the function
fetchUpdateAsyncexposed to us is not a promise but rather contains fetch promise and returns its result.So, here we have two options:
But note that updates will be fetched in background and stored on the device. Next time whenever user restarts the app, update will be installed. I think this is just fine as this approach doesn't block anything for user and also there is a default timeout for http request clients like fetch and axios so, request will error out in case of poor/no internet connection.
Here is the code:
Here you can return a cancel method with
Updates.fetchUpdateAsync()and use it withsetTimeoutto cancel the fetch request. I won't be providing any code for this part but if you are curious I can definitely provide some help.Please refer this section to understand use of
fallbackToCacheTimeoutin eas updates.Old solution:
Now, for aborting or bypassing the promise i.e.
Updates.fetchUpdateAsyncin your case. You can basically throw anErrorinsetTimeoutafter whatever time duration you want, so that, catch block will be executed, bypassing the promises. Here is the old code :