对于模拟承诺没有任何意义(承诺)

时间:2017-10-30 08:38:18

标签: javascript promise

我浏览了其他类似帖子,他们都提到我需要在我的承诺中指定catch块而不是第二个参数(onSuccessonRejected)。

但是我知道如果我将myPromise.then(onSuccess, onRejected)更改为myPromise.then(onSuccess).catch(onRejected),那么catch块永远不会被执行,错误仍会显示在chrome dev控制台中。



// Verifiers
const verifyOnFulfilled = (result) => {
  expect(result).toEqual(expectedOnFulfilledResponse);
};

const verifyOnRejected = (result) => {
  expect(result).toEqual(expectedOnRejectedResponse);
};

// Mocking a server call and this catch statement does nothing.
// Added just to test if this fixes the prob
mockPromise.catch((e) => {
  return e;
});

// Mock request bind. Ignore this
requestSpy.and.returnValue(mockPromise);

// Call the api
const promiseA = myService.deleteItem(id); // It will create a new mock promise
promiseA.then(verifyOnFulfilled, verifyOnRejected);
// promiseA.then(verifyOnFulfilled).catch(verifyOnRejected); // If I use this then catch function is never executed.

// Run mock promises
MockPromises.executeForPromise(promiseA);




API调用块:



let deleteRequestPromise = ... // some internal generic way to create promise

  return deleteRequestPromise.then(
    () => {
      return commentId;
    },
    (error) => {
      return Promise.reject(error);
    }
  );




0 个答案:

没有答案