Home > Blockchain >  How to cancel pending promise calls inside callback function?
How to cancel pending promise calls inside callback function?

Time:06-07

fetch(
  "https://rqyhfmwv4b.execute-api.us-east-1.amazonaws.com/production/join-meeting",
  requestOptions
).then((response) => response.json())
.then(async (data) => {
  // You need responses from server-side Chime API. See below for details.
  const meetingResponse =
  data.result
  .meetingResponsee; /* The response from the CreateMeeting API action */
  const attendeeResponse =
  data.result
  .attendeeResponsee; /* The response from the CreateAttendee or BatchCreateAttendee API action */
  const configuration = new MeetingSessionConfiguration(
    meetingResponse,
    attendeeResponse
  );
  
  // In the usage examples below, you will use this meetingSession object.
  const meetingSession = new DefaultMeetingSession(
    configuration,
    logger,
    deviceController
  );
  
  const meetingReadinessChecker = new DefaultMeetingReadinessChecker(
    logger,
    meetingSession
  );
  
  // testing
  
  if (pauseReadinessTest) {
    setPauseReadinessTest(false);
    return;
  }
  
  if (testParam == "All" && disableStartRedinessTest !== true) {
    await audioTest(deviceController);
    setMeetingTestProgress(15);
    await micTest(meetingSession, meetingReadinessChecker, CheckAudioInputFeedback);
    setMeetingTestProgress(25);
    await videoTest(meetingSession, meetingReadinessChecker, CheckVideoInputFeedback);
    await cameraTest(meetingSession, meetingReadinessChecker, CheckCameraResolutionFeedback);
    setMeetingTestProgress(50);
    await networkTest(meetingReadinessChecker, CheckNetworkTCPConnectivityFeedback, CheckNetworkUDPConnectivityFeedback);
    setMeetingTestProgress(75);
    await streamTest(meetingSession, meetingReadinessChecker, CheckAudioConnectivityFeedback, CheckVideoConnectivityFeedback);
    setMeetingTestProgress(85);
    await screenShareTest(meetingSession, meetingReadinessChecker, CheckContentShareConnectivityFeedback);
    setMeetingTestProgress(100);
    setRefreshIconClass(" ");
    SetDisableStartRedinessTest(false);
    setIsResult("result");
}

I have this block of code and I want to cancel the pending await function calls if at any point in time the user wants to exit from the test. But the issue is whenever the value of a variable gets updated it doesn't reflect inside the block which is causing the main issue over here.

CodePudding user response:

As far as I remember, promise concept is mostly a syntax sugar. What happens in real world - you pass a callback function (B) inside a function call (A) and at some moment function A will do a call to function B. If you did not write function A with canceling concept in mind, there is no way to cancel the call. Easier to use some other method (global variable, extra passed object argument, whatever) to set a flag "do not run" and check it inside function B.

CodePudding user response:

If you have a pending promise inside a callback function, you can cancel it by using the Promise.prototype.cancel() method. For example:

var promise = new Promise(function(resolve, reject) {
  // do something

  if (/* some condition */) {
    resolve(); // or reject(), depending on your needs
  } else {
    promise.cancel(); // this will cancel the pending promise and prevent it from being resolved or rejected
  }
});

CodePudding user response:

You could do something like this:

const promises = [{
    functionCall: () => audioTest(deviceController), 
    progress: 15,
}, {
    functionCall: await micTest(meetingSession, meetingReadinessChecker, CheckAudioInputFeedback),
    progress: 25,
    }, // ...etc.
];

while (testParam == "All" && disableStartRedinessTest !== true && promises.length) {
    const { functionCall, progress } = promises.unshift();
    await functionCall.call();
    setMeetingTestProgress(progress);
}

Although if you're thinking about cancelling operations, I would use RxJS instead of vanilla promises.

  • Related