Home  >  Article  >  Web Front-end  >  Can ECMAScript 6 Promises Be Cancelled?

Can ECMAScript 6 Promises Be Cancelled?

Susan Sarandon
Susan SarandonOriginal
2024-10-25 05:31:29746browse

Can ECMAScript 6 Promises Be Cancelled?

Canceling ECMAScript 6 Promise Chains

Is there a way to remove the .thens from a JavaScript Promise instance?

ECMAscript 6 Promises are not cancellable by default. Despite ongoing discussions, there is no official solution in ES6.

Alternatives:

  • Subclassing: Creating your own Promise implementation allows for cancellation, but its implementation may be unreliable.
  • Userland Promise implementations: Third-party libraries like cancelable-promise and promise-cancellation offer cancellable Promises.

Current Solution for Unblocking Tests:

Implement a custom cancelTest flag within the test framework that polls for cancellation requests. When the timeout occurs, set cancelTest to true and call assert.fail() to mark the test as failed.

Further Updates:

After resolving the initial issue (an uncaught error during fn.call()), the then() statement was not being called due to an uncaught error in the test function itself. By catching the error, the test promise can resolve gracefully, allowing the test to finish and the then() statement to execute.

The above is the detailed content of Can ECMAScript 6 Promises Be Cancelled?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn