Note: since I can't add comment, I posted this answer.
const sleep = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
// perform some task here
resolve(true);
}, timeout);
});
};
async () => {
// in asynchronous function
await sleep(1000);
// do something after code returns
};
// in global scope or synchronous
sleep(1000).then(() => {
// callback
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…