We are moving to Discord!

Join us on the Countly Community Discord Server! Engage in discussions, share your feature ideas, and learn from fellow Countly users to build exceptional apps and experiences.

Is there a way to make sure all requests to server from NodeJS SDK are finished

We are building a CLI app and there is always a problem that NodeJS process finishes before all requests to Countly server are finished so, e.g. errors are never sent because I am not able to find some callback or promise or something similar that would tell me that all requests are finished and it is safe to finish the process. The only way to actually send errors for me is to just wait an arbitrary number of seconds (using setTimeout) after I do


and only then finish the process execution

Could you please help me understand what can be done in this situation?


  • Official comment

    Hi Artsiom,
    Thank you for letting us about the situation you are experiencing. Unfortunately the situation you have described is not a use-case we have some solutions for. As Countly NodeJS SDK is intended to run in long hauls only thing that can give information about the inner state of the SDK is the SDK logs. Other than that it would all depend on your inner logic to decide when to end the processes (like in your case you use timeout, someone else might try to intercept or listen to the requests and implement their own logic etc.).

    However we take these feedback seriously and would consider discussing the issue in our future product cycles. Thank you for your time and information.

    Comment actions Permalink
  • Just to clarify:

    If error happens early - session start also has no time to reach server so nothing is sent to server at all!

    And I can't use Countly.track_errors() because the framework that I use for CLI development (oclif - it's one of the most popular ones for NodeJS) catches and handles errors itself, so that is why I use Countly.log_error(err) inside catch instead of Countly.track_errors()

    Comment actions Permalink
  • If anyone else is in the same situation - use @mswjs/interceptors lib to intercept the requests from Countly. I wanted to have a Promise that would let me know when event's are delivered to Countly. So I created it and it looks something like this:

    let resolveErrorPromise;
    let hasCrashed = false;
    let hasSentCommandEvent = false;
    const createErrorPromise = (errorHandler) =>
    newPromise((resolve) => {
    resolveErrorPromise = () =>resolve(errorHandler());
    const {
    } = require("@mswjs/interceptors/lib/interceptors/ClientRequest");
    const interceptor = new ClientRequestInterceptor();
    interceptor.on("response", (_response, request) => {
    if (request.url.includes("/i?crash=")) {
    hasCrashed = true;
    if (hasSentCommandEvent) {

    if (
    request.url.includes("/i?events=") &&
    ) {
    hasSentCommandEvent = true;
    if (hasCrashed) {
    Comment actions Permalink

Please sign in to leave a comment.