如何 运行 javascript 中的 setInterval 中的异步函数
how to run an async func in setInterval in javascript
如何在 setInterval
中 运行 异步函数,如下例所示?
setInterval(async () => {
}, millisPerHour);
尝试使用此代码
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: "resolved"
}
asyncCall();`
如何在 setInterval
中 运行 异步函数,如下例所示?
setInterval(async () => {
}, millisPerHour);
尝试使用此代码
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: "resolved"
}
asyncCall();`