Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bind.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
  3. var calledBind = false;
  4. var rejectThis = function(_, e) {
  5. this._reject(e);
  6. };
  7. var targetRejected = function(e, context) {
  8. context.promiseRejectionQueued = true;
  9. context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
  10. };
  11. var bindingResolved = function(thisArg, context) {
  12. if (((this._bitField & 50397184) === 0)) {
  13. this._resolveCallback(context.target);
  14. }
  15. };
  16. var bindingRejected = function(e, context) {
  17. if (!context.promiseRejectionQueued) this._reject(e);
  18. };
  19. Promise.prototype.bind = function (thisArg) {
  20. if (!calledBind) {
  21. calledBind = true;
  22. Promise.prototype._propagateFrom = debug.propagateFromFunction();
  23. Promise.prototype._boundValue = debug.boundValueFunction();
  24. }
  25. var maybePromise = tryConvertToPromise(thisArg);
  26. var ret = new Promise(INTERNAL);
  27. ret._propagateFrom(this, 1);
  28. var target = this._target();
  29. ret._setBoundTo(maybePromise);
  30. if (maybePromise instanceof Promise) {
  31. var context = {
  32. promiseRejectionQueued: false,
  33. promise: ret,
  34. target: target,
  35. bindingPromise: maybePromise
  36. };
  37. target._then(INTERNAL, targetRejected, undefined, ret, context);
  38. maybePromise._then(
  39. bindingResolved, bindingRejected, undefined, ret, context);
  40. ret._setOnCancel(maybePromise);
  41. } else {
  42. ret._resolveCallback(target);
  43. }
  44. return ret;
  45. };
  46. Promise.prototype._setBoundTo = function (obj) {
  47. if (obj !== undefined) {
  48. this._bitField = this._bitField | 2097152;
  49. this._boundTo = obj;
  50. } else {
  51. this._bitField = this._bitField & (~2097152);
  52. }
  53. };
  54. Promise.prototype._isBound = function () {
  55. return (this._bitField & 2097152) === 2097152;
  56. };
  57. Promise.bind = function (thisArg, value) {
  58. return Promise.resolve(value).bind(thisArg);
  59. };
  60. };