1. //Promise
    2. const sleep = time => {
    3. return new Promise(resolve => setTimeout(resolve,time))
    4. }
    5. sleep(1000).then(()=>{
    6. console.log('sleep')
    7. })
    8. //Generator
    9. function* sleepGenerator(time) {
    10. yield new Promise(function(resolve,reject){
    11. setTimeout(resolve,time);
    12. })
    13. }
    14. sleepGenerator(1000).next().value.then(()=>{ console.log('sleep')})
    15. //async
    16. function sleep(time) {
    17. return new Promise(resolve => setTimeout(resolve,time))
    18. }
    19. async function output() {
    20. let out = await sleep(1000);
    21. console.log('sleep')
    22. return out;
    23. }
    24. output();
    25. //ES5
    26. function sleep(callback,time) {
    27. if(typeof callback === 'function')
    28. setTimeout(callback,time)
    29. }
    30. function output(){
    31. console.log('sleep');
    32. }
    33. sleep(output,1000);