-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dns.promises.Resolver
does not have a .cancel()
method
#33091
Comments
Yes, just mount the |
I'm not sure can add cancel in the promise API directly. Reference:21264#discussion_r196582158 /cc @cjihrig |
It can be done, but people have opinions there. I'm personally OK with it, but you should probably check with others. |
We cannot add a cancel to the Promises API directly but there are ways cancel can be accomplished... The following is an example from a workshop I deliver to customers: const EventEmitter = require('events');
// When using Race or All, if the underlying async API
// provides a mechanism for cancelation the pending
// operation, then a signaling mechanism like event
// emitter can be used to interject the ability to
// cancel. This, however, can be subject to race
// conditions that are not unlike thread synchronization
function createPromise(monitor, timeout, value) {
return new Promise((res, rej) => {
const t = setTimeout(() => {
res(value);
monitor.emit('resolved');
}, timeout);
monitor.on('resolved', () => {
clearTimeout(t);
rej('canceled')
});
});
}
function Foo(monitor) {
return createPromise(monitor, 1000, 'B');
}
function Bar(monitor) {
return createPromise(monitor, 10, 'A');
}
const monitor = new EventEmitter();
Promise.race([Foo(monitor), Bar(monitor)]).then(console.log); Specifically, while Promises themselves cannot be canceled, if the underlying mechanism being used to schedule the asynchronous activity provides a means for canceling, then |
If I'm remembering the linked discussion correctly, the issue was just about using the name "cancel" not actually implementing promise cancellation. |
So I see no reason why can't we just |
When user code is using That is, this would work: const { Resolver } = require('dns').promises;
const resolver = new Resolver();
resolver.setServers(['4.4.4.4']);
resolver.resolve4('example.org').then((addresses) => {});
resolver.cancel(); But canceling this would not: const { resolve4 } = require('dns').promises;
resolve4('example.org').then((addresses) => {}); |
You can't cancel this either. What's your point? const { resolve4 } = require('dns');
resolve4('example.org', (error, addresses) => {}); |
If we need to be worried about cancelling these, it should be another issue... |
Be explicitly clear about expectations. I'm agreeing with you that implementing this should be possible. Just needs a PR. |
Is your feature request related to a problem? Please describe.
You cannot cancel pending DNS queries using
dns.promises.Resolver
. Note thatdns.Resolver
has a.cancel()
method, but the async version does not.Describe alternatives you've considered
There are no alternatives yet.
The text was updated successfully, but these errors were encountered: