是的,它本身没有提供 ,不管使用第三方或者使用代理的形式,都会在其他使用的地方大量穿插
做非法判断就行
还可以补充几个 比如操作必须按照顺序执行的时候用就特别方便
async processOrder(orderId) {
await validateOrder(orderId);
await processPayment(orderId);
await updateInventory(orderId);
await sendConfirmation(orderId);
}
这种就不好 会顺序执行影响效率
async fetchAllData() {
const users = await fetchUsers();
const products = await fetchProducts();
return { users, products };
}
换成这样并行执行就行了
async fetchAllData() {
const [users, products] = await Promise.all([
fetchUsers(),
fetchProducts()
]);
return { users, products };
}
但是比如单词的时候其实没必要使用比如
async simpleFetch() {
const response = await fetch(’/api/data’);
return response.json();
}
这个写法其实不如
simpleFetch() {
return fetch(’/api/data’).then(response => response.json());
}
还有一个 我经常用的小技巧 解决await reject的问题
public static awaitTo(promise: Promise) {
if (promise) {
return promise
.then(data => [null, data])
.catch(err => [err, null]);
}
else {
Promise.resolve()
}
}
封装一个后用起来贼简洁
这是两回事,虽然在这个问题下,可能想解决的是一回事