I am using hedera-sdk-js,
version 2.37.0, installed from @hashgraph/sdk
on NPM.
When I run a script in NodeJs, the process remains open indefinitely,
and in order to force it to terminate,
I have been using process.exit(0);
.
This works, but is not ideal - I would prefer that it terminates gracefully.
Is there a way to do this?
Details:
Here is the current script
import dotenv from 'dotenv';
import {
Client,
AccountId,
PrivateKey,
} from '@hashgraph/sdk';
dotenv.config();
main();
async function main() {
if (!process.env.ACCOUNT_ID ||
!process.env.ACCOUNT_PRIVATE_KEY) {
throw new Error('Please set required keys in .env file.');
}
const accountId = AccountId.fromString(process.env.ACCOUNT_ID);
const accountKey = PrivateKey.fromStringECDSA(process.env.ACCOUNT_PRIVATE_KEY);
const client = Client.forTestnet().setOperator(accountId, accountKey);
// ... do some things here ...
process.exit(0); // <-- NOTE would like to remove this
}
The
Client
instance is keeping the Javascript event loop open. For a graceful shutdown of your script, you simply need to stop theClient
instance, using.close()
.Here's the modified version of your code example, only one line has changed: