Quantcast
Channel: 小小博客
Viewing all articles
Browse latest Browse all 16

js同步执行延迟

$
0
0
function delay(n) {
  return new Promise(function(resolve) {
    setTimeout(resolve, n * 1000);
  });
}

async function myAsyncFunction() {
  // Do what you want here
  console.log('Before the delay')

      await delay(5);

  console.log('After the delay')
  // Do what you want here too
}

myAsyncFunction();

利用 await 来实现同步等待


Viewing all articles
Browse latest Browse all 16

Trending Articles