这为将来添加了一个成功/错误处理程序,例如:
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)
# ?????
}
慕田峪9158850
相关分类