admin管理员组文章数量:1410689
I created a library for Node and the browser and want to call an external endpoint which responds with a stream. Since this stream might never end the client should be able to abort the process.
public async observe(): Promise<() => void> {
const abortController = new AbortController();
const response = await fetch('url', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: { /* ... */ },
signal: abortController.signal,
});
if (!response.ok) {
// handle error
}
setImmediate(async () => {
// parse response.body ( stream )
});
return () => {
abortController.abort();
};
}
I used setImmediate
because I must return the abort function first, otherwise this function would never reach the return statement because it keeps parsing the stream.
Unfortunately setImmediate
is not supported by browsers ( #browser_patibility )
Does someone know what to use instead?
I created a library for Node and the browser and want to call an external endpoint which responds with a stream. Since this stream might never end the client should be able to abort the process.
public async observe(): Promise<() => void> {
const abortController = new AbortController();
const response = await fetch('url', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: { /* ... */ },
signal: abortController.signal,
});
if (!response.ok) {
// handle error
}
setImmediate(async () => {
// parse response.body ( stream )
});
return () => {
abortController.abort();
};
}
I used setImmediate
because I must return the abort function first, otherwise this function would never reach the return statement because it keeps parsing the stream.
Unfortunately setImmediate
is not supported by browsers ( https://developer.mozilla/en-US/docs/Web/API/Window/setImmediate?retiredLocale=de#browser_patibility )
Does someone know what to use instead?
Share Improve this question asked Oct 25, 2022 at 14:28 baitendbidzbaitendbidz 8054 gold badges22 silver badges71 bronze badges 2- try-finally? Equally cursed. – k.tten Commented Oct 25, 2022 at 14:30
- 1 Why not just use setTimeout with a timeout of zero? – slebetman Commented Oct 25, 2022 at 14:32
1 Answer
Reset to default 8There are a couple of options.
If you want a microTask you could use a Promise.
Promise.resolve().then(
() => console.log('there'));
console.log('hello');
For a normal Task, you could use setTimeout
;
setTimeout(() => console.log('there'));
console.log('hello');
There is also the requestAnimationFrame
. https://developer.mozilla/en-US/docs/Web/API/window/requestAnimationFrame
It's been pointed out why not just use try finally
. But one issue with that, if your processing the stream in finally, then by the time the caller gets the abort
function it's too late. But rather than using something like setImmediate
, it would actually be cleaner if you just return a promise for the streaming, and the abort function together.
eg.
public async observe(): Promise<() => void> {
const abortController = new AbortController();
const response = await fetch('url', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: { /* ... */ },
signal: abortController.signal,
});
if (!response.ok) {
// handle error
}
return {
abort: () => abortController.abort(),
promise: async () => {
// parse response.body ( stream )
}
};
}
You can then call like ->
const {abort, promise} = observe();
setTimeout(() => abort(), 5000); //abort in 5 seconds
await promise;
本文标签: javascriptWhat is the setImmediate() equivalent that is supported by browsersStack Overflow
版权声明:本文标题:javascript - What is the setImmediate() equivalent that is supported by browsers? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744802323a2625937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论