zoukankan      html  css  js  c++  java
  • await 暂停 等待 暂停的是什么

    体验异步的终极解决方案-ES7的Async/Await

    var sleep = function (time) {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve();
            }, time);
        })
    };
    
    var start = async function () {
        // 在这里使用起来就像同步代码那样直观
        console.log('start');
        await sleep(3000);
        console.log('end');
    };
    
    start();


    体验异步的终极解决方案-ES7的Async/Await - CNode技术社区 https://cnodejs.org/topic/5640b80d3a6aa72c5e0030b6




    对比


    var sleep = function (time) {
    return new Promise(function (resolve, reject) {
    setTimeout(function () {
    resolve();
    }, time);
    })
    };

    var start = async function () {
    // 在这里使用起来就像同步代码那样直观
    console.log('start');
    await sleep(3000);
    console.log('end');
    };

    start();
    console.log('AFTER start();');

    function resolveAfter2Seconds() {
    return new Promise(resolve => {
    setTimeout(() => {
    resolve('resolved');
    }, 10000);
    });
    }

    async function asyncCall() {
    console.log('calling');
    var result = await resolveAfter2Seconds();
    console.log(result);
    // expected output: 'resolved'
    }

    asyncCall();




  • 相关阅读:
    链式栈的C++实现
    Java面试之设计模式二
    前端资源
    Java面试之异常
    Java面试之序列化
    Java面试之重写(Override)与重载(Overload)
    项目视图展示
    Java面试之集合
    Java面试之SSH框架面试题集锦
    JDBC技术
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9563571.html
Copyright © 2011-2022 走看看