zoukankan      html  css  js  c++  java
  • es6中 async await 底层实现原理

    Input
    async function findPosts() {
      var response = await $.get('/posts');
      return JSON.parse(response.posts);
    }
    
    async function main() {
      console.log('starting...');
      
      var posts = await findPosts();
    
      posts.forEach(function (post) {
        console.log(post);
      });
      
      console.log('ending...');
    }
    
    main();
    
    Output
    function findPosts() {
        var ctx = this, args = arguments;
        return Promise.resolve().then(function () {
            var response;
            return $.get('/posts').then(function (value) {
                response = value;
                return JSON.parse(response.posts);
            });
        });
    }
    
    function main() {
      var ctx = this, args = arguments;
      return Promise.resolve().then(function () {
        console.log('starting...');
        
        var posts;
        return findPosts().then(function (value) {
          posts = value;
          
          posts.forEach(function (post) {
            console.log(post);
          });
          
          console.log('ending...');
        });
      });
    }
    
    main();
    

      

    我很想 知道他是如何  

    var posts = await findPosts(); 
    后面的代码  也添加到 同一个
     return findPosts().then(  方法中的


    参考  https://github.com/jayphelps/sweet-async-await

  • 相关阅读:
    两个51相互之间单片机如何进行串口通信
    (stm32f103学习总结)—stm32pwm
    (stm32f103学习总结)—stm32 PMW输出实验
    cpu指令如何读写硬盘
    线程进程同步
    stm32+lwip
    opc
    open62541-server编程
    linux 下time函数
    close与shutdown
  • 原文地址:https://www.cnblogs.com/FlowLight/p/11694114.html
Copyright © 2011-2022 走看看