Set Request timeout

Is there a way to set a maximum wait time for a Request to complete? I’d like to set a request to timeout after 2 seconds of no response.

try out this: delay time in milliseconds

async function timeout() {
var delay = 5000;
var before = Date.now();
while (Date.now() < before + delay) {};
return true;
}
timeout().then( () => {
console.log(‘hello’); })

That looks like code for a time delay script rather than a way to specify a timeout on a system/network request.

Thanks for you reply @SvenK. As @sylumer mentioned that’s not quite what I’m after. This is the functionality I’m after which doesn’t seem to be currently supported.

let request = new Request('http://google.com/')

//request.load() will timeout the request after 1000ms and not continue waiting for a response.
request.timeout = 1000

let response = await request.load()

This looks like a feature request as there is nothing like this in Scriptable AFAIK.

@simonbs, could you please have a look and maybe implement a request timeout?

I’ve just added this for the next update. Note that the timeout interval will be measured in seconds.

4 Likes

This does not technically timeout the request but has the same effect.

const request = new Request('https://google.com/');
const result = await Promise.any([request.loadString(), wait(1000)]);
console.log(result);

function wait(ms) {
    return new Promise((resolve) => Timer.schedule(ms, false, resolve));
}
2 Likes

@anyhotcountry this is great and does exactly what I was after. I think I’ll wait for the “official” timeout functionality to come out now though. But Thankyou!