@
lisxour #5
@
tangmou 代码忘了贴了:
```javascriipt
// 需要订购的产品链接
const productList = [
"https://xxx",
"https://xxx",
"https://xxx"
]
async function asyncSleep(duration) {
await new Promise(r => setTimeout(r, duration));
}
// 是否有货
async function hasStock(url) {
await asyncSleep(2000);
return false
}
// 开始订购
const startOrder = async () => {
const promises = productList.map(u => ({tag: u, promise: hasStock(u)}))
const results = await Promise.allSettled(promises)
for (const [i, result] of results.entries()) {
// 是否有货,有就订购
if (!result.value) {
console.log("无货", promises[i].tag)
continue
}
}
}
function exitHandler(options, exitCode) {
console.log("process exit:", performance.now());
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
console.log("before:", performance.now());
startOrder();
console.log("after:", performance.now());
```