taylorhakes/promise-polyfill

Promise._immediateFn usage BUG

Closed this issue · 2 comments

when handle function call Promise._immediateFn ,the parameter function should be wrapped in IIFE.

function handle(self, deferred) {
  while (self._state === 3) {
    self = self._value;
  }
  if (self._state === 0) {
    self._deferreds.push(deferred);
    return;
  }
  self._handled = true;
//----------------//
// var "self" changed when  muliti task called , the callback function should be wrapped in IIFE
//----------------//
  Promise._immediateFn(function() {
    var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
    if (cb === null) {
      (self._state === 1 ? resolve : reject)(deferred.promise, self._value);
      return;
    }
    var ret;
    try {
      ret = cb(self._value);
    } catch (e) {
      reject(deferred.promise, e);
      return;
    }
    resolve(deferred.promise, ret);
  });
}

// Solution:

  Promise._immediateFn((function(self) {
    var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
    if (cb === null) {
      (self._state === 1 ? resolve : reject)(deferred.promise, self._value);
      return;
    }
    var ret;
    try {
      ret = cb(self._value);
    } catch (e) {
      reject(deferred.promise, e);
      return;
    }
    resolve(deferred.promise, ret);
  })(self));

Thanks for reporting this bug. There is a test suite attached to project. Would you be able to create a failing test or provider code that you are seeing the bug?

I am closing this PR. Please respond with more info and I will reopen