@@ -410,7 +410,7 @@ function ts_loader_trampoline_test(obj) {
410410 }
411411}
412412
413- function ts_loader_trampoline_await ( trampoline ) {
413+ function ts_loader_trampoline_await_function ( trampoline ) {
414414 if ( ! trampoline ) {
415415 return function ts_loader_trampoline_await_impl ( func , args , trampoline_ptr ) {
416416 console . error ( 'TypeScript Loader await error, trampoline could not be found, await calls are disabled.' ) ;
@@ -433,9 +433,39 @@ function ts_loader_trampoline_await(trampoline) {
433433 return new Promise ( ( resolve , reject ) =>
434434 func ( ...args ) . then (
435435 x => resolve ( trampoline . resolve ( trampoline_ptr , x ) ) ,
436- x => reject ( trampoline . reject ( trampoline_ptr , x ) ) ,
436+ x => reject ( trampoline . reject ( trampoline_ptr , x ) )
437437 ) . catch (
438- x => console . error ( `NodeJS await error: ${ x && x . message ? x . message : util . inspect ( x , false , null , true ) } ` ) ,
438+ x => console . error ( `TypeScript await error: ${ x && x . message ? x . message : util . inspect ( x , false , null , true ) } ` )
439+ )
440+ ) ;
441+ } ;
442+ }
443+
444+ function ts_loader_trampoline_await_future ( trampoline ) {
445+ if ( ! trampoline ) {
446+ return function ts_loader_trampoline_await_impl ( func , args , trampoline_ptr ) {
447+ console . error ( 'TypeScript Loader await error, trampoline could not be found, await calls are disabled.' ) ;
448+ } ;
449+ }
450+
451+ return function ts_loader_trampoline_await_impl ( future , trampoline_ptr ) {
452+ // This apparently does not work for native promises, let it uncommented until we find a proper way of detecting the type
453+ /*
454+ if (!!future && typeof future.then === 'function') {
455+ throw new Error('Await only accepts a thenable promise, not ' + typeof future);
456+ }
457+ */
458+
459+ if ( typeof trampoline_ptr !== 'object' ) {
460+ throw new Error ( 'Await trampoline_ptr must be an object, not ' + typeof trampoline_ptr ) ;
461+ }
462+
463+ return new Promise ( ( resolve , reject ) =>
464+ future . then (
465+ x => resolve ( trampoline . resolve ( trampoline_ptr , x ) ) ,
466+ x => reject ( trampoline . reject ( trampoline_ptr , x ) )
467+ ) . catch (
468+ x => console . error ( `TypeScript await error: ${ x && x . message ? x . message : util . inspect ( x , false , null , true ) } ` )
439469 )
440470 ) ;
441471 } ;
@@ -488,7 +518,8 @@ module.exports = ((impl, ptr) => {
488518 'clear' : ts_loader_trampoline_clear ,
489519 'discover' : ts_loader_trampoline_discover ,
490520 'test' : ts_loader_trampoline_test ,
491- 'await' : ts_loader_trampoline_await ( trampoline ) ,
521+ 'await_function' : ts_loader_trampoline_await_function ( trampoline ) ,
522+ 'await_future' : ts_loader_trampoline_await_future ( trampoline ) ,
492523 'destroy' : ts_loader_trampoline_destroy ,
493524 } ) ;
494525 } catch ( ex ) {
0 commit comments