NodeJs使用asyncAwait两法

2017-06-17 18:33:17

async/await使用同步的方式来书写异步代码,将异步调用的难度降低到接近于0,未来必将大放异彩。然而在当下,由于标准化的缓存步伐,async/await尚在ES7的草案中。为了尝先,特试用了下面两种方式:

  • 使用社区提供的asyncawait封装

  • 使用ES7草案

使用社区提供的asyncawait模块

Git地址

git@github.com:yortus/asyncawait.git

使用方法:

1.       安装node模块

a)         npm install asyncawait@1.0.3 –save

2.       创建示例类AsyncService.js

var async = require('asyncawait/async');
var await = require('asyncawait/await');
var sleep = async(
    
function sleep(timeout) {
        
return new Promise(function (resolve, reject) {
            
setTimeout(function () {
                resolve();
            }, timeout);
        });
    }
);

(
async(
    
function () {
        
console.log('Do some thing, ' + new Date());
        
await(sleep(3000));
        
console.log('Do other things, ' + new Date());
    }
))();

3.       运行AsyncService.js

a)         node AsyncService.js

b)         运行结果:

Do some thing, Wed Jun 15 2016 11:09:05 GMT+0800 (中国标准时间)

Do other things, Wed Jun 15 2016 11:09:08 GMT+0800 (中国标准时间)

注意事项

1.       asyncawait模块内部引用bluebird模块.

2.       无须编译为Es5,可直接运行.

使用ES7草案

使用方法:

1.       安装node模块,需要的一系列模块如下:

a)    babel-cli
b)    babel-preset-es2015"
c)    babel-preset-react":
d)    babel-preset-stage-3
e)    babel-polyfill

2.       创建示例类 AsyncAwaitService.js

async function sleep(timeout) {
   
return new Promise((resolve, reject) => {
       
setTimeout(function () {
            resolve();
        }, timeout);
    });
}

(
async function () {
   
console.log('Do some thing, ' + new Date());
   
await sleep(3000);
   
console.log('Do other things, ' + new Date());
})();

                  

3.       编译AsyncAwaitService.js

a)         配置babel

                         i.              在package.json中加入babel节点,内容如下:

 

"babel": {
 
"presets": [
   
"es2015",
   
"react",
   
"stage-3"
 
],
 
"plugins": []
}

          

b)         编译

babel AsyncAwaitService.js --out-file AsyncAwaitService_es5.js
或者
babel AsyncAwaitService.js -o AsyncAwaitService_es5.js

c)         标记编译后的代码

     在AsyncAwaitService_es5.js脚本头部加入以下代码:
require('babel-polyfill')

4.       运行AsyncAwaitService_es5.js

a)         node AsyncAwaitService_es5.js

b)         运行结果:

Do some thing, Wed Jun 15 2016 11:54:13 GMT+0800 (中国标准时间)

Do other things, Wed Jun 15 2016 11:54:16 GMT+0800 (中国标准时间)

注意事项

1.       async/await通过babel编译为Es5,方可直接运行.

2.       babel编译相关内容可参考阮一峰博客 http://www.ruanyifeng.com/blog/2016/01/babel.html


  • 2019-09-19 09:07:46

    @Autowired用法详解

    在使用@Autowired时,首先在容器中查询对应类型的bean     如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据     如果查询的结果不止一个,那么@Autowired会根据名称来查找。     如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false

  • 2019-09-19 11:36:58

    注解@Mapper、@MapperScan

    定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类

  • 2019-09-19 11:46:45

    @EnableConfigurationProperties注解详解,源码

    用springboot开发的过程中,我们会用到@ConfigurationProperties注解,主要是用来把properties或者yml配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。 如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的,当然在@ConfigurationProperties加入注解的类上加@Component也可以使交于springboot管理。

  • 2019-09-19 14:10:03

    Android UI布局优化之ViewStub介绍

    ViewStub的inflate只能被调用一次,第二次调用会抛出异常,setVisibility可以被调用多次,但不建议这么做(文章中说原因)

  • 2019-09-19 14:21:47

    Dubbo和spring cloud微服务框架区别和介绍

    关于 Dubbo 和 Spring Cloud 的相关概念和对比,上面已经叙述的很清楚了,我个人比较倾向于 Spring Cloud,原因就是真正的微服务框架、提供整套的组件支持、使用简单方便、强大的社区支持等等,另外,因为考虑到 .NET/.NET Core 的兼容处理,RPC 并不能很好的实现跨语言(需要借助跨语言库,比如 gRPC、Thrift,但因为 Dubbo 本身就是“gRPC”,在 Dubbo 之上再包一层 gRPC,有点重复封装了),而 HTTP REST 本身就是支持跨语言实现,所以,Spring Cloud 这一点还是非常好的(Dubbox 也支持,但性能相比要差一些)。