Hey,
Speaking to library authors in the ecosystem it appears that this pattern of code is very common (also in our code):
if(signal.aborted){// cleanup}else{constlistener=(err)=>{// cleanup };signal.addEventListener('abort',listener);resource.on('done',()=>signal.removeEventListener('abort',listener);}It would be very useful to be able to write this code in a more ergonomic way, talking to @getify about this in the CAF repo a utility was suggested:
const{ once }=require('events');// returns a promise for when the signal was abortedasyncfunctionaborted(signal,resource=null){if(signal.aborted)return;// early return on aborted signal// the kWeak bit not implemented yet is so that the event listener doesn't leakawaitonce(signal,"abort",{[kWeak]: resource});}Which would let you do
awaitaborted(signal);// Or// or some other resource, when the request gets GCd the listener gets removed automaticallyawaitaborted(signal,request);
Any opinions on this? (Personally I am in favour) If we add such an API under what module would it live?
cc @jasnell@addaleax ?
Hey,
Speaking to library authors in the ecosystem it appears that this pattern of code is very common (also in our code):
It would be very useful to be able to write this code in a more ergonomic way, talking to @getify about this in the CAF repo a utility was suggested:
Which would let you do
Any opinions on this? (Personally I am in favour) If we add such an API under what module would it live?
cc @jasnell@addaleax ?