Reorder code, fix build errors
							parent
							
								
									4a316bb7ca
								
							
						
					
					
						commit
						a00c98679a
					
				@ -0,0 +1,45 @@
 | 
			
		||||
import { promisify } from "util";
 | 
			
		||||
import AbortablePromise from ".";
 | 
			
		||||
 | 
			
		||||
const sleep = promisify((timeout: number, callback: (...args: any[]) => void) =>
 | 
			
		||||
  setTimeout(callback, timeout)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
const a = new AbortablePromise(async function* () {
 | 
			
		||||
  yield await sleep(1000);
 | 
			
		||||
  console.log("awaited 100");
 | 
			
		||||
  yield await sleep(2000);
 | 
			
		||||
  console.log("awaited 200");
 | 
			
		||||
  yield await sleep(3000);
 | 
			
		||||
  console.log("awaited 300");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
setTimeout(() => a.abort(), 1500);
 | 
			
		||||
 | 
			
		||||
function abortableSleep(ms: number): AbortablePromise<void> {
 | 
			
		||||
  return new AbortablePromise(async function* () {
 | 
			
		||||
    await sleep(ms);
 | 
			
		||||
    yield;
 | 
			
		||||
    console.log(`Slept ${ms}.`);
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const b = AbortablePromise.deadlyRace([
 | 
			
		||||
  abortableSleep(1000),
 | 
			
		||||
  abortableSleep(2000),
 | 
			
		||||
  abortableSleep(3000),
 | 
			
		||||
]);
 | 
			
		||||
 | 
			
		||||
// const b = new AbortablePromise(async function* (await_or_abort) {
 | 
			
		||||
//   const ping = await deferedSpawn("ping", ["8.8.8.8"]);
 | 
			
		||||
//   while (true) {
 | 
			
		||||
//     console.log(
 | 
			
		||||
//       await await_or_abort(ping.waitForNextData(), () => {
 | 
			
		||||
//         ping.kill();
 | 
			
		||||
//       })
 | 
			
		||||
//     );
 | 
			
		||||
//     yield;
 | 
			
		||||
//   }
 | 
			
		||||
// });
 | 
			
		||||
 | 
			
		||||
// setTimeout(() => b.abort(), 5000);
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue