-1

I have a function where I do this :

this._getContainerData().then((data) => {
    this.__getIndexes(idx).forEach((indexVisible, index) => {
        this.containerData[indexVisible] = data[index]
    });
    if (this.horizontalRailIndex === idx) {
        this.container._setData(this.context);
    }
});

this._getContainerData() returns a promise and it's called multiple times. I want to check if this promise takes more than 1000ms - if so cancel it or return an empty promise. I have a class called CancelablePromise that I can import and use like : new CancelablePromise and it does have a cancel function. If you have any other suggestions I will be glad to hear them.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • There is [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) but it doesn't cancel anything. – VLAZ Mar 08 '21 at 15:22
  • ok but how can i put a timeout on this._getContainerData() before to use promise.race after – Ahmed-Fadhel Achour Mar 08 '21 at 15:24
  • 1
    https://stackoverflow.com/questions/32461271/nodejs-timeout-a-promise-if-failed-to-complete-in-time - it's not 100% overlap, but i am not a personal google-bot. – ASDFGerte Mar 08 '21 at 15:26
  • 2
    Tl;dr; you construct a promise, and resolve/reject it either, when the actual promise resolves, or from a timeout. – ASDFGerte Mar 08 '21 at 15:29
  • Does this answer your question? [NodeJS Timeout a Promise if failed to complete in time](https://stackoverflow.com/questions/32461271/nodejs-timeout-a-promise-if-failed-to-complete-in-time) – Louys Patrice Bessette Mar 08 '21 at 15:46
  • nope actually i have a cancel function that will cancel my promise and i want to use instead of doing what mentioned in the link – Ahmed-Fadhel Achour Mar 08 '21 at 16:19
  • So you expose the reject and trigger it – epascarello Mar 09 '21 at 01:22

1 Answers1

0

Demo1

import { CPromise } from "c-promise2";

const _getContainerData = () =>
  new CPromise((resolve, reject, { onCancel }) => {
    const timer = setTimeout(resolve, 2000, "myValue");

    onCancel((reason) => {
      console.log(`your cancel routine: ${reason}`);
      clearTimeout(timer);
    });
  });

_getContainerData()
  .timeout(1000)
  .then((data) => {
    console.log("then code");
  });

Demo2

import { CPromise, promisify } from "c-promise2";

const _getContainerData = promisify(function* (scope) {
  scope.onCancel((reason) => {
    console.log(`your cancel routine: ${reason}`);
  });
  // inner async routines
  console.log("start");
  yield new Promise((resolve) => setTimeout(resolve, 300));
  console.log("stage 1");
  yield new Promise((resolve) => setTimeout(resolve, 300));
  console.log("stage 2");
  yield new Promise((resolve) => setTimeout(resolve, 300));
  console.log("stage 3");
  yield new Promise((resolve) => setTimeout(resolve, 300));
  console.log("stage 4");
  yield new Promise((resolve) => setTimeout(resolve, 300));
  return "myValue";
});

const promise = _getContainerData()
  .timeout(1000)
  .then(
     data => console.log(`complete with: ${data}`), 
     err => console.warn(`Failed: ${err}`)
  );

// manual cancellation
// promise.cancel();
Dmitriy Mozgovoy
  • 1,419
  • 2
  • 8
  • 7