shim.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. var microtaskDelay = require("../../../../function/#/microtask-delay");
  3. if (typeof Promise !== "function") global.Promise = require("plain-promise");
  4. module.exports = function (t, a) {
  5. return {
  6. Success: function (d) {
  7. var invoked;
  8. t.call(Promise.resolve("foo"), function () {
  9. invoked = true;
  10. return "bar";
  11. }).then(
  12. microtaskDelay.call(function (result) {
  13. a(result, "foo");
  14. a(invoked, true);
  15. d();
  16. }, microtaskDelay.call(d))
  17. );
  18. },
  19. Failure: function (d) {
  20. var invoked;
  21. var error = new Error("Some error");
  22. t.call(Promise.reject(error), function () {
  23. invoked = true;
  24. return "bar";
  25. }).then(
  26. microtaskDelay.call(function () {
  27. a.never();
  28. d();
  29. }),
  30. microtaskDelay.call(function (result) {
  31. a(result, error);
  32. a(invoked, true);
  33. d();
  34. })
  35. );
  36. },
  37. SuccessFinallyError: function (d) {
  38. var invoked, finallyError = new Error("Finally error");
  39. t.call(Promise.resolve("foo"), function () {
  40. invoked = true;
  41. throw finallyError;
  42. }).then(
  43. microtaskDelay.call(function () {
  44. a.never();
  45. d();
  46. }),
  47. microtaskDelay.call(function (result) {
  48. a(result, finallyError);
  49. a(invoked, true);
  50. d();
  51. })
  52. );
  53. },
  54. FailureFinallyError: function (d) {
  55. var invoked, finallyError = new Error("Finally error");
  56. t.call(Promise.reject(new Error("Some error")), function () {
  57. invoked = true;
  58. throw finallyError;
  59. }).then(
  60. microtaskDelay.call(function () {
  61. a.never();
  62. d();
  63. }),
  64. microtaskDelay.call(function (result) {
  65. a(result, finallyError);
  66. a(invoked, true);
  67. d();
  68. })
  69. );
  70. }
  71. };
  72. };