对于async,await进行封装

由于async,await只能获取到promise成功状态的值,不能捕获到失败的值,这里进行封装一下,避免链式调用catch方法

type asynchronousFun = (value: Promise<any>) => Promise<any[]>
const asynchronous: asynchronousFun = (param) =>{
	return new Promise(function (resolve) {
    const result: any[] = [];
    Promise.resolve(param).then(function (successResult) {
      result[0] = successResult;
    }, function (errorResult) {
      result[1] = errorResult;
    }).finally(function () {
      resolve(result);
    });
  });
}
export default asynchronous

你可能感兴趣的:(js原生重难点知识点,javascript,typescript,es6)