节点:等到函数完成后再执行 process.exit

Node: wait till function is finished before executing process.exit

我是节点的新手。我写了一个执行telnet 的脚本。如果 telnet 有效,我们将执行 traceroute 并正确退出(这有效)。当 telnet 不起作用时,我仍然执行 traceroute 并尝试以退出状态 -1 退出。这是行不通的。没有显示 traceroute 并且 process.exit(1) 也出现了 early/does 没有等到 traceroute 的输出。

这是我的脚本(它是一个 lambda):

'use strict';
const Traceroute = require('nodejs-traceroute');
const isPortReachable = require('is-port-reachable');

console.log('Loading function');
process.env.PATH = `${process.env.PATH}:${process.env.LAMBDA_TASK_ROOT}/bin`;


async function doTraceroute() {
    try {
        const tracer = new Traceroute();
        tracer
            .on('pid', (pid) => {
                console.log(`pid: ${pid}`);
            })
            .on('destination', (destination) => {
                console.log(`destination: ${destination}`);
            })
            .on('hop', (hop) => {
                console.log(`hop: ${JSON.stringify(hop)}`);
            })
            .on('close', (code) => {
                console.log(`close: code ${code}`);
            });
    
        tracer.trace('100.216.125.9');
    } catch (ex) {
        console.log(ex);
    }
}

exports.handler = (event, context, callback) => {
    (async () => {
        console.log('Is port 9092 reachable of MC Kafka host? True or False')
        let output = await isPortReachable(9092, {host: '100.216.125.9'})
        if (output) {
            console.log('Telnet worked: do traceroute')
            await doTraceroute();
        } else {
            console.log('Telnet failed: do traceroute')
            await doTraceroute();
            console.log("Exit with error")
            process.exit(1)
        }
    })();
};

我该如何解决? telnet失败时的当前输出为:

START RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79 Version: $LATEST
2020-11-09T07:25:07.841Z    315b0ac5-09d7-4b72-8ff7-746b28241f79    INFO    Is port 9092 reachable of MC Kafka host? True or False
2020-11-09T07:25:08.851Z    315b0ac5-09d7-4b72-8ff7-746b28241f79    INFO    Telnet failed: do traceroute
2020-11-09T07:25:08.909Z    315b0ac5-09d7-4b72-8ff7-746b28241f79    INFO    pid: 20
2020-11-09T07:25:08.910Z    315b0ac5-09d7-4b72-8ff7-746b28241f79    INFO    Exit with error
END RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79
REPORT RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79  Duration: 1150.28 ms    Billed Duration: 1200 ms    Memory Size: 128 MB Max Memory Used: 70 MB  Init Duration: 176.80 ms    
RequestId: 315b0ac5-09d7-4b72-8ff7-746b28241f79 Error: Runtime exited with error: exit status 1
Runtime.ExitError

不显示跟踪路由。 telnet成功没有出口1时显示traceroute

您可以将 traceroute 处理包装在一个 promise 中并等待这个 promise:

function doTraceroute() {
    return new Promise((resolve, reject) => {
        try {
            const tracer = new Traceroute();
            tracer
                .on('pid', (pid) => {
                    console.log(`pid: ${pid}`);
                })
                .on('destination', (destination) => {
                    console.log(`destination: ${destination}`);
                })
                .on('hop', (hop) => {
                    console.log(`hop: ${JSON.stringify(hop)}`);
                })
                .on('close', (code) => {
                    console.log(`close: code ${code}`);
                    resolve();
                });

            tracer.trace('100.216.125.9');
        } catch (ex) {
            console.log(ex);
            reject(ex);
        }
    });
}

然后您可以 await 返回的承诺:

await doTraceroute();