diff --git a/packages/cubejs-client-core/src/index.ts b/packages/cubejs-client-core/src/index.ts index d20bdeb9bb91b..51326a44a9e76 100644 --- a/packages/cubejs-client-core/src/index.ts +++ b/packages/cubejs-client-core/src/index.ts @@ -150,14 +150,20 @@ let mutexCounter = 0; const MUTEX_ERROR = 'Mutex has been changed'; -function mutexPromise(promise: Promise) { - return promise - .then((result) => result) - .catch((error) => { - if (error !== MUTEX_ERROR) { - throw error; - } - }); +class MutexCancelledError extends Error { + constructor() { + super(MUTEX_ERROR); + this.name = 'MutexCancelledError'; + } +} + +function mutexPromise(promise: Promise): Promise { + return promise.catch((error) => { + if (error === MUTEX_ERROR) { + throw new MutexCancelledError(); + } + throw error; + }); } export type ResponseFormat = 'compact' | 'default' | undefined; @@ -412,7 +418,14 @@ class CubeApi { return subscribeNext(); }; - const promise = requestPromise.then(requestInstance => mutexPromise(requestInstance.subscribe(loadImpl))); + const promise = requestPromise + .then(requestInstance => mutexPromise(requestInstance.subscribe(loadImpl))) + .catch((error) => { + if (error instanceof MutexCancelledError) { + return null; + } + throw error; + }); if (callback) { return {