TypeScript type definition for promise.reject

送分小仙女□ 提交于 2019-12-05 00:08:59

You should not return Promise.resolve and Promise.reject inside a promise chain. The resolve should be driven by simple return and reject should be driven by an explicit throw new Error.

Promise.resolve(['one', 'two'])
.then( arr =>
{
  if( arr.indexOf('three') === -1 )
    throw new Error('Where is three?');

  return arr;
})
.catch( err =>
{
  console.log(err); // Error: where is three?
})

More

More on promise chaining https://basarat.gitbooks.io/typescript/content/docs/promise.html

Typescript is complaining about the difference in return type between your Promise.reject return value (Promise<void>) and your Promise.resolve value (Promise<string[]>).

Casting your then call as .then<Promise<void | string[]>> will let the compiler know of the union return type.

as @basarat notes, you should just throw an error instead of using Promise.reject (which will be passed to whatever catch handler is provided).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!