const awaitWrap = (promise) => {
return promise
.then(data => [null, data])
.catch(err => [err, null]);
}
const fn1 = () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log('fn1')
resolve("fn1 √")
}, 2000)
})
}
const fn2 = () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log('fn2')
resolve("fn2 √")
}, 2000)
})
}
const fn3 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('fn3')
reject("fn3 ×")
}, 2000)
})
}
async function fna() {
let a = await fn1();
let b = await fn2();
console.log(a + b);
}
async function fnb() {
let a1 = fn1();
let b1 = fn2();
let a = await a1;
let b = await b1;
console.log(a + b);
}
async function fnc() {
let [err, data] = await awaitWrap(fn3());
if (err)
//do something
console.log('err', err);
console.log('data', data);
}
// fna();
// fnb();
fnc();
// function awaitWrap<T, U = any>(promise: Promise<T>): Promise<[U | null, T | null]> {
// return promise
// .then<[null, T]>((data: T) => [null, data])
// .catch<[U, null]>(err => [err, null])
// }
console