repl.js
function wrapReplEval(replServer) {
  const defaultEval = replServer.eval;

  return function(code, context, file, cb) {
    return defaultEval.call(this, code, context, file, (err, result) => {
      if (!result || !result.then) {
        return cb(err, result);
      }

      result.then(resolved => {
        resolvePromises(result, resolved);
        cb(null, resolved);
      }).catch(err => {
        resolvePromises(result, err);

        console.log('\x1b[31m' + '[Promise Rejection]' + '\x1b[0m');
        if (err && err.message) {
          console.log('\x1b[31m' + err.message + '\x1b[0m');
        }

        // Application errors are not REPL errors
        cb(null, err);
      });
    });

    function resolvePromises(promise, resolved) {
      Object.keys(context).forEach(key => {
        // Replace any promise handles in the REPL context with the resolved promise
        if (context[key] === promise) {
          context[key] = resolved;
        }
      });
    }
  };
}
function wrapReplEval(replServer) {
  const defaultEval = replServer.eval;

  return function(code, context, file, cb) {
    return defaultEval.call(this, code, context, file, (err, result) => {
      if (!result || !result.then) {
        return cb(err, result);
      }

      result.then(resolved => {
        resolvePromises(result, resolved);
        cb(null, resolved);
      }).catch(err => {
        resolvePromises(result, err);

        console.log('\x1b[31m' + '[Promise Rejection]' + '\x1b[0m');
        if (err && err.message) {
          console.log('\x1b[31m' + err.message + '\x1b[0m');
        }

        // Application errors are not REPL errors
        cb(null, err);
      });
    });

    function resolvePromises(promise, resolved) {
      Object.keys(context).forEach(key => {
        // Replace any promise handles in the REPL context with the resolved promise
        if (context[key] === promise) {
          context[key] = resolved;
        }
      });
    }
  };
}
function wrapReplEval ( replServer ) { const defaultEval = replServer . eval ; return function ( code , context , file , cb ) { return defaultEval . call ( this , code , context , file , ( err , result ) => { if ( ! result || ! result . then ) { return cb ( err , result ) ; } result . then ( resolved => { resolvePromises ( result , resolved ) ; cb ( null , resolved ) ; } ) . catch ( err => { resolvePromises ( result , err ) ; console . log ( '\x1b[31m' + '[Promise Rejection]' + '\x1b[0m' ) ; if ( err && err . message ) { console . log ( '\x1b[31m' + err . message + '\x1b[0m' ) ; } // Application errors are not REPL errors cb ( null , err ) ; } ) ; } ) ; function resolvePromises ( promise , resolved ) { Object . keys ( context ) . forEach ( key => { // Replace any promise handles in the REPL context with the resolved promise if ( context [ key ] === promise ) { context [ key ] = resolved ; } } ) ; } } ; }
Wrap the default eval with a handler that resolves promises
Wrap the default eval with a handler that resolves promises