timeout.js 839 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. var delay = require("../../delay");
  3. module.exports = function (t, a) {
  4. if (typeof Promise !== "function") return null;
  5. return {
  6. Success: function (d) {
  7. var promise = t.call(
  8. new Promise(function (resolve) {
  9. setTimeout(function () { resolve("foo"); }, 20);
  10. }),
  11. 40
  12. );
  13. promise.then(
  14. // Delay to escape error swallowing
  15. delay(function (result) {
  16. a(result, "foo");
  17. d();
  18. }),
  19. delay(d)
  20. );
  21. },
  22. Timeout: function (d) {
  23. var promise = t.call(
  24. new Promise(function (resolve) {
  25. setTimeout(function () { resolve("foo"); }, 40);
  26. }),
  27. 20
  28. );
  29. promise.then(
  30. // Delay to escape error swallowing
  31. delay(function () {
  32. a.never();
  33. d();
  34. }),
  35. delay(function (err) {
  36. a(err.code, "PROMISE_TIMEOUT");
  37. d();
  38. })
  39. );
  40. }
  41. };
  42. };