【发布时间】:2019-12-16 08:27:00
【问题描述】:
这会为将来添加一个成功/错误处理程序,例如:
async function send(item) {
// ...
}
for (const item of items) {
const sendPromise = send(item);
sendPromise
.then(x => console.log(x))
.catch(x => console.error(x))
}
而不是等待:
for (const item of items) {
const sendPromise = send(item);
try {
const x = await sendPromise
console.log(x)
} catch (e) {
console.error(e)
}
}
什么是 python 的 Task 相当于 JS 的 promise.then() 没有等待?
async def send(item):
pass
for item of items:
send_coro = send(item)
send_task = asyncio.create_task(send_coro)
# ?????
}
【问题讨论】:
标签: python promise async-await python-asyncio coroutine