Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 440 Bytes

异步相关.md

File metadata and controls

26 lines (24 loc) · 440 Bytes

#Promise

var p = new Promise((res,rej)=>{
  const value = 'value of request'
  setTimeout(function(){
    res(value)
  },3000)
  }
)
p.then(function(data){console.log(`request done with ${data}`)})

#async

function timeout(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

async function asyncPrint(value, ms) {
  await timeout(ms);
  console.log(value);
}

asyncPrint('hello world', 3000);