'How can I close two asynchronous connections once they are done in node.js

programming community. I'm running two asynchronous functions but I want to close them both once they are done. They both are executing upsert operations to tables in the salesforce database. This is what currently looks like:

const firstHalfOfTheList = [] //Array of records to upsert
const secondHalfOfTheList = [] //Array of records to upsert   
//FIRST OPERATION
conn.bulk.load("Object_table__c", "upsert", { extIdField: "External_ID__c" }, firstHalfOfTheList, (err, rets) => {
if (err) {
    conn.sobject("Integration_Log__c").create(
        {
            Type__c: "Error",
            Message__c: rets[i].errors.join(", "),
            Full_Error__c: rets[i].errors.join(", "),
        },
        (err, ret) => {
            if (err || !ret.success) {
                logger.error(err, ret);
            } else {
                logger.info("Created error log record");
            }
        }
    );
} else {
    conn.sobject("Integration_Log__c").create(
        {
            Type__c: "Info",
            Message__c: `${numberSucessRecords} Records upserted in target system`,
            Status__c: "Success",
        },
        (err, ret) => {
            if (err || !ret.success) {
                logger.error(err, ret);
            } else {
                logger.info("Created success log record");
            }
        }
    );
}
});

//SECOND OPERATION
conn.bulk.load("Object_table__c", "upsert", { extIdField: "External_ID__c" }, secondHalfOfTheList, (err, rets) => {
if (err) {
    conn.sobject("Integration_Log__c").create(
        {
            Type__c: "Error",
            Message__c: rets[i].errors.join(", "),
            Full_Error__c: rets[i].errors.join(", "),
        },
        (err, ret) => {
            if (err || !ret.success) {
                logger.error(err, ret);
            } else {
                logger.info("Created error log record");
            }
        }
    );
} else {
    conn.sobject("Integration_Log__c").create(
        {
            Type__c: "Info",
            Message__c: `${numberSucessRecords} Records upserted in target system`,
            Status__c: "Success",
        },
        (err, ret) => {
            if (err || !ret.success) {
                logger.error(err, ret);
            } else {
                logger.info("Created success log record");
            }
        }
    );
}
});

I tried to add process.exit() at the end like this but it didn't work because it closed the function that was done first:

conn.bulk.load(
      ...
      ...
      ...
      process.exit()
    )


conn.bulk.load(
      ...
      ...
      ...
      process.exit() 
     )

I would like to know how can I close the script ONCE BOTH processes are done. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source