import { isAbortError , timeout } from "./index" ; import TaskInstance from "./instance" ; import { AnyFunction } from "./types" ; export default async function perform < F extends AnyFunction > ( task , args ) { await timeout ( 0 ) ; let result = task . fn ( ... args ) ; if ( result && typeof result . next === "function" ) { let isFinished = false , lastResolvedValue ; const generator = task . fn ( ... args ) ; while ( ! isFinished ) { if ( task . current . isCancelled ) { return ; } try { const { value , done } = generator . next ( lastResolvedValue ) ; if ( value instanceof TaskInstance ) { task . abortController . signal . addEventListener ( "abort" , ( ) => { value . abortController . abort ( ) ; } ) ; } lastResolvedValue = await value ; isFinished = done ; } catch ( e ) { if ( isAbortError ( e ) ) { task . abortController . abort ( ) ; } else { task . reject ( e ) ; } return ; } } result = lastResolvedValue ; } else { result = await result ; } task . resolve ( result ) ; }