@@ -288,7 +288,7 @@ function node_loader_trampoline_test(obj) {
288
288
}
289
289
}
290
290
291
- function node_loader_trampoline_await ( trampoline ) {
291
+ function node_loader_trampoline_await_function ( trampoline ) {
292
292
if ( ! trampoline ) {
293
293
return function node_loader_trampoline_await_impl ( func , args , trampoline_ptr ) {
294
294
console . error ( 'NodeJS Loader await error, trampoline could not be found, await calls are disabled.' ) ;
@@ -311,9 +311,39 @@ function node_loader_trampoline_await(trampoline) {
311
311
return new Promise ( ( resolve , reject ) =>
312
312
func ( ...args ) . then (
313
313
x => resolve ( trampoline . resolve ( trampoline_ptr , x ) ) ,
314
- x => reject ( trampoline . reject ( trampoline_ptr , x ) ) ,
314
+ x => reject ( trampoline . reject ( trampoline_ptr , x ) )
315
315
) . catch (
316
- x => console . error ( `NodeJS await error: ${ x && x . message ? x . message : util . inspect ( x , false , null , true ) } ` ) ,
316
+ x => console . error ( `NodeJS await error: ${ x && x . message ? x . message : util . inspect ( x , false , null , true ) } ` )
317
+ )
318
+ ) ;
319
+ } ;
320
+ }
321
+
322
+ function node_loader_trampoline_await_future ( trampoline ) {
323
+ if ( ! trampoline ) {
324
+ return function node_loader_trampoline_await_impl ( func , args , trampoline_ptr ) {
325
+ console . error ( 'NodeJS Loader await error, trampoline could not be found, await calls are disabled.' ) ;
326
+ } ;
327
+ }
328
+
329
+ return function node_loader_trampoline_await_impl ( future , trampoline_ptr ) {
330
+ // This apparently does not work for native promises, let it uncommented until we find a proper way of detecting the type
331
+ /*
332
+ if (!!future && typeof future.then === 'function') {
333
+ throw new Error('Await only accepts a thenable promise, not ' + typeof future);
334
+ }
335
+ */
336
+
337
+ if ( typeof trampoline_ptr !== 'object' ) {
338
+ throw new Error ( 'Await trampoline_ptr must be an object, not ' + typeof trampoline_ptr ) ;
339
+ }
340
+
341
+ return new Promise ( ( resolve , reject ) =>
342
+ future . then (
343
+ x => resolve ( trampoline . resolve ( trampoline_ptr , x ) ) ,
344
+ x => reject ( trampoline . reject ( trampoline_ptr , x ) )
345
+ ) . catch (
346
+ x => console . error ( `NodeJS await error: ${ x && x . message ? x . message : util . inspect ( x , false , null , true ) } ` )
317
347
)
318
348
) ;
319
349
} ;
@@ -360,7 +390,8 @@ module.exports = ((impl, ptr) => {
360
390
'discover' : node_loader_trampoline_discover ,
361
391
'discover_function' : node_loader_trampoline_discover_function ,
362
392
'test' : node_loader_trampoline_test ,
363
- 'await' : node_loader_trampoline_await ( trampoline ) ,
393
+ 'await_function' : node_loader_trampoline_await_function ( trampoline ) ,
394
+ 'await_future' : node_loader_trampoline_await_future ( trampoline ) ,
364
395
'destroy' : node_loader_trampoline_destroy ,
365
396
} ) ;
366
397
} catch ( ex ) {
0 commit comments