node-libcurl

    Class Multi

    Multi class that acts as an wrapper around the native libcurl multi handle.

    C++ source code

    Using this class instead of just the Easy allows one to run requests asynchronously.

    For usage see examples/04-multi.js

    The Curl class uses one of this internally to provide asynchronous usage of the library.

    Index

    Constructors

    • Creates a new Multi instance.

      Parameters

      • Optionaloptions: MultiOptions

        Optional configuration for the Multi instance

      Returns Multi

      // Use default settings (notifications enabled if libcurl >= 8.17.0)
      const multi = new Multi()

      // Explicitly enable notifications (recommended for libcurl >= 8.17.0)
      const multiWithNotifications = new Multi({ shouldUseNotificationsApi: true })

      // Use traditional polling (compatible with all libcurl versions)
      const multiWithPolling = new Multi({ shouldUseNotificationsApi: false })

    Methods

    • Closes this multi handle.

      After the handle has been closed it must not be used again.

      This is basically the same than curl_multi_cleanup()

      Returns void

    • Returns the number of 'Easy' handles that are currently inside this instance

      Returns number

    • Allow to provide a callback that will be called when there are new information about the handles inside this instance.

      This is basically an abstraction over curl_multi_info_read()

      Pass null to remove the current callback set.

      Parameters

      • cb: null | ((error: Error, easyHandle: Easy, errorCode: CurlCode) => void)

      Returns this

      This will be eventually removed in favor of just using perform to add handles to the multi handle, instead of using addHandle.

    • Adds an Easy handle to this Multi instance and returns a promise that resolves when the request completes successfully, or rejects with a CurlError if it fails.

      This is the modern, promise-based alternative to using addHandle with onMessage.

      The returned promise will:

      • Resolve with the Easy handle when the request completes successfully
      • Reject with a CurlError (containing a code property with the CurlCode value) on failure

      Parameters

      • handle: Easy

        The Easy handle to perform the request with

      Returns Promise<Easy>

      A promise that resolves with the Easy handle or rejects with a CurlError

      const multi = new Multi()
      const easy = new Easy()
      easy.setOpt('URL', 'https://example.com')

      try {
      await multi.perform(easy)
      console.log('Request completed successfully')
      } catch (error) {
      console.error('Request failed with code:', error.code)
      }

      This does what curl_multi_add_handle() does.

    MMNEPVFCICPMFPCPTTAAATR