From d6f99f57b7351ea4ad447076dd4dbd3cdd782e64 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Thu, 28 Mar 2019 11:12:32 +0100 Subject: [PATCH] Auto resolve promises in repl --- scripts/repl.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scripts/repl.js b/scripts/repl.js index fcce559..5929e0b 100644 --- a/scripts/repl.js +++ b/scripts/repl.js @@ -5,6 +5,21 @@ const Kredits = require('../lib/kredits'); const getNetworkId = require('./helpers/networkid.js') +function promiseEval (repl) { + const currentEval = repl.eval; + return function (cmd, context, filename, callback) { + currentEval(cmd, context, filename, (err, result) => { + if (result && typeof result.then === 'function') { + console.log('...waiting for promise to resolve'); + return result + .then(response => callback(null, response)) + .catch(err => callback(err, null)); + } + return callback(err, result); + }) + } +} + module.exports = async function(callback) { const networkId = await getNetworkId(web3) const provider = new ethers.providers.Web3Provider( @@ -16,6 +31,7 @@ module.exports = async function(callback) { let r = REPL.start(); r.context.kredits = kredits; r.context.web3 = web3; + r.eval = promiseEval(r); r.on('exit', () => { console.log('Bye');