@@ -410,7 +410,7 @@ function ts_loader_trampoline_test(obj) {
410
410
}
411
411
}
412
412
413
- function ts_loader_trampoline_await ( trampoline ) {
413
+ function ts_loader_trampoline_await_function ( trampoline ) {
414
414
if ( ! trampoline ) {
415
415
return function ts_loader_trampoline_await_impl ( func , args , trampoline_ptr ) {
416
416
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) {
433
433
return new Promise ( ( resolve , reject ) =>
434
434
func ( ...args ) . then (
435
435
x => resolve ( trampoline . resolve ( trampoline_ptr , x ) ) ,
436
- x => reject ( trampoline . reject ( trampoline_ptr , x ) ) ,
436
+ x => reject ( trampoline . reject ( trampoline_ptr , x ) )
437
437
) . 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 ) } ` )
439
469
)
440
470
) ;
441
471
} ;
@@ -488,7 +518,8 @@ module.exports = ((impl, ptr) => {
488
518
'clear' : ts_loader_trampoline_clear ,
489
519
'discover' : ts_loader_trampoline_discover ,
490
520
'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 ) ,
492
523
'destroy' : ts_loader_trampoline_destroy ,
493
524
} ) ;
494
525
} catch ( ex ) {
0 commit comments