如何将 async/await 与 Promises 结合起来?
How to combine async/await with Promises?
在下面的代码中,我在 async
函数中设置了 createPost()
,但我不想在其上使用 await
,因此它会阻止其余部分的执行功能。这就是为什么我使用 then
.
外部 async
函数有很多其他函数使用 await
,因此无法更改。
问题
因为 createPost()
是一个 async
/await
函数(见下文)。外层函数怎么可以resolve/reject到then
/catch
?
module.exports = async (p) => {
// lots of code here
const t = await function1();
// lots of code here
createPost(p).then(message => {
// lots of code here
console.log("ok " + message);
}).catch(message => {
console.log("failed " + message);
});
};
createPost()
module.exports = async (p) => {
// lots of code here
try {
const r = await getStat();
} catch (error) {
console.log(error);
};
};
async
/ await
是 .then
/ .catch
.
更现代的等价物
在这种情况下,您混合了两者的语法。
使用 .then
、
时不需要 try
/ catch
块
就像在使用 try
/ catch
语法时不需要将函数声明为 async
一样。
除此之外,他们没有理由不能在不同的职能部门一起工作。
module.exports = (p) => {
createPost(p).then(message => {
console.log("ok " + message);
}).catch(message => {
console.log("failed " + message);
});
};
createPost():
module.exports = async (p) => {
try {
const r = await getStat();
} catch (error) {
console.log(error);
};
};
如果您没有在函数中使用 await
关键字,则对函数的 async
声明是多余的和错误的。
await/async 通常被称为语法糖,让我们等待一些东西(例如 API 调用),给我们一种错觉,它在实际的异步代码中是同步的,这是一个很大的好处。
你想用 async/await 实现的事情是可以通过承诺实现的,但是 async/await 的优势。让我们用这段代码举例:
const makeRequest = () => //promise way
getJSON()
.then(data => {
return data
})
makeRequest();
const makeRequest = async () => { //async await way
const data = await getJSON();
return data;
}
makeRequest()
为什么 async/await 优于 promise?
简洁明了 - 我们不必编写 .then 并创建匿名函数来处理响应,或将名称数据提供给我们不需要使用的变量。我们还避免了嵌套我们的代码。 async/await 干净多了。
错误处理 - Async/await 最终可以使用相同的 try/catch 格式处理同步和异步错误。
调试 - 使用 async/await 的一个非常好的优势是调试比承诺更容易,原因有两个:1) 你可以't 在 return 表达式(无正文)的箭头函数中设置断点。 2) 如果你在 .then 块中设置断点并使用调试快捷方式,如 step-over,调试器将不会移动到下面的 .then 因为它只“步进”同步代码。
错误堆栈 - 从 promises 链中 return 得到的错误堆栈让我们不知道错误发生的位置并且可能会产生误导。 async/await 为我们提供了从 async/await 指向包含错误的函数的错误堆栈,这是一个非常大的优势。
在下面的代码中,我在 async
函数中设置了 createPost()
,但我不想在其上使用 await
,因此它会阻止其余部分的执行功能。这就是为什么我使用 then
.
外部 async
函数有很多其他函数使用 await
,因此无法更改。
问题
因为 createPost()
是一个 async
/await
函数(见下文)。外层函数怎么可以resolve/reject到then
/catch
?
module.exports = async (p) => {
// lots of code here
const t = await function1();
// lots of code here
createPost(p).then(message => {
// lots of code here
console.log("ok " + message);
}).catch(message => {
console.log("failed " + message);
});
};
createPost()
module.exports = async (p) => {
// lots of code here
try {
const r = await getStat();
} catch (error) {
console.log(error);
};
};
async
/ await
是 .then
/ .catch
.
更现代的等价物
在这种情况下,您混合了两者的语法。
使用 .then
、
时不需要 try
/ catch
块
就像在使用 try
/ catch
语法时不需要将函数声明为 async
一样。
除此之外,他们没有理由不能在不同的职能部门一起工作。
module.exports = (p) => {
createPost(p).then(message => {
console.log("ok " + message);
}).catch(message => {
console.log("failed " + message);
});
};
createPost():
module.exports = async (p) => {
try {
const r = await getStat();
} catch (error) {
console.log(error);
};
};
如果您没有在函数中使用 await
关键字,则对函数的 async
声明是多余的和错误的。
await/async 通常被称为语法糖,让我们等待一些东西(例如 API 调用),给我们一种错觉,它在实际的异步代码中是同步的,这是一个很大的好处。
你想用 async/await 实现的事情是可以通过承诺实现的,但是 async/await 的优势。让我们用这段代码举例:
const makeRequest = () => //promise way
getJSON()
.then(data => {
return data
})
makeRequest();
const makeRequest = async () => { //async await way
const data = await getJSON();
return data;
}
makeRequest()
为什么 async/await 优于 promise?
简洁明了 - 我们不必编写 .then 并创建匿名函数来处理响应,或将名称数据提供给我们不需要使用的变量。我们还避免了嵌套我们的代码。 async/await 干净多了。
错误处理 - Async/await 最终可以使用相同的 try/catch 格式处理同步和异步错误。
调试 - 使用 async/await 的一个非常好的优势是调试比承诺更容易,原因有两个:1) 你可以't 在 return 表达式(无正文)的箭头函数中设置断点。 2) 如果你在 .then 块中设置断点并使用调试快捷方式,如 step-over,调试器将不会移动到下面的 .then 因为它只“步进”同步代码。
错误堆栈 - 从 promises 链中 return 得到的错误堆栈让我们不知道错误发生的位置并且可能会产生误导。 async/await 为我们提供了从 async/await 指向包含错误的函数的错误堆栈,这是一个非常大的优势。