async

async函数

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:01:52
一.什么是async函数 1. 概念 async函数是co(Generator)函数的语法糖。 语法是将Generator函数的*替换为async; yield替换为await; var fnName = async function() { let result = await Promise.resolve(1); console.log(result); //1 } fnName(); // 相当于 const co = require('co'); co(function* fnName() { let result = yield Promise.resolve(1); console.log(result); //1 }) async函数相当于执行器+Generator函数,原理如下: async function fn() { //.... } // 模拟源码实现 function fn() { // run+Generator--等同于co + Generator return run(function* (){ }) } function run(fn) { return new Promise((resolve,reject) => { const gen = fn(); function next(nextFn) { let next; try { next

log4j日志异步化大幅提升系统性能

∥☆過路亽.° 提交于 2019-12-03 15:12:17
经过大型J2EE项目实测,Log4j的日志输出对系统性能有比较显著的影响,尤其是日志输入量比较大时,例如:系统并发量很大,显示Hibernate的sql和参数日志,或日志级别较低DEBUG或INFO时等。 使用Async Logger控制日志输出可以显著改善系统性能。 1) 测试用例:同步文件日志输出 测试功能:模块管理功能, 输出Hibernate SQL和参数,输出到文件,配置如下: <appender name="Hibernate_SQL" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="log/Hibernate_SQL.log" /> <param name="Append" value="true" /> <param name="DatePattern" value="'.'yyyy-MM-dd-HH" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%-5p](%C,%L) - %m%n" /> </layout> </appender> <category name="org.hibernate.SQL">

Async/Await是这样简化JavaScript代码的

余生长醉 提交于 2019-12-03 13:38:23
译者按: 在 Async/Await替代Promise的6个理由 中,我们比较了两种不同的异步编程方法: Async/Await 和 Promise ,这篇博客将通过示例代码介绍 Async/Await 是如何简化JavaScript代码的。 原文: ASYNC/AWAIT WILL MAKE YOUR CODE SIMPLER 译者: Fundebug 为了保证可读性,本文采用意译而非直译。另外,本文版权归原作者所有,翻译仅用于学习。 Async/Await 是JavaScript的 ES7 新特性,来源于**.NET 和 C# 。它可以不用回调函数,像同步代码那些编写异步代码。这篇博客将通过一些代码示例,来说明 Async/Await**如何简化JavaScript代码。 1. 去除回调函数 运行本文的示例代码,并不需要额外的函数库。对于最新版的主流浏览器中,例如Chrome,Firefox, Safari以及Edge,它们都支持Async/Await语法。另外,Node.js 7.6+也支持了Async/Await语法。 我们编写了一些简单的API接口,用于模拟异步操作。这些接口都返回Promise,并在200ms后 resolve 一些数据。 class Api { constructor () { this.user = { id: 1, name: 'test' }

async/await 与 Promise 详解

时光总嘲笑我的痴心妄想 提交于 2019-12-03 11:03:34
async 是什么? async 中文翻译过来是 “异步,非同步,异步通信” 的意思。 async 是 ES7 才有的 与异步操作有关 的关键字,和 Promise , Generator 有很大关联的。 注:ES7/8/9 都是 ES6 的补充 怎么用? 语法 : async function name([param[, param[, ... param]]]) { statements } // name: 函数名称。 // param: 要传递给函数的参数的名称。 // statements: 函数体语句。 返回值 :   async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数。 async function helloAsync(){ return "helloAsync"; } console.log(helloAsync()) // Promise {<resolved>: "helloAsync"} helloAsync().then(v=>{ console.log(v); // helloAsync }) 既然说到了 async 函数,那就必然少不了要说说 await 。 await 是什么? await 中文翻译过来的意思是 “等待,暂停函数的执行,等待异步任务完成” async 函数中 可能 会有 await 表达式,async

nodejs的C++扩展中实现异步回调

断了今生、忘了曾经 提交于 2019-12-03 10:21:38
在nodejs的官方网站中有关于C++扩展的详细说明,其中包含了从"hello world"到对象封装的一系列示例。其中的“ callback ”节是关于回调函数的,美中不足的是,这个回调是阻塞的回调。 官方示例的回调函数用JS代码来模拟的话,大致是这个样子: function syncCallback(callback) { // 业务代码 // 业务代码 callback(); } 使用C++扩展的一个最大好处就是处理一些CPU密集的业务,因此这部分代码一定是比较耗时的,否则用C++去实现完全没有意义。业务代码中的阻塞操作,例如传统文件读写、密集计算等都会导致nodejs原始线程的阻塞,导致后来的请求无法得到及时响应,严重影响node的并发性能。 有服务器程序开发的朋友肯定已经想到用多线程的方法解决这个问题。是的,我要分享的就是在C++扩展中用多线程的方法处理回调,从而达到解决复杂的业务同时保证node线程的无阻塞特性。 node C++扩展中,可以使用libuv提供的线程方法,非常方便的进行线程调度。 下面是具体代码,详细解释见注释: #include <v8.h> #include <node.h> #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> using

Async function not working as intended when called at onInit lifecycle hook

匿名 (未验证) 提交于 2019-12-03 10:10:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: In my project I have a service that should load local database when the app is started. For that the function GetData() is used. I try to use it by calling at the ngOnInit() lifecycle hook. It logs the result in the console, but the property datum appears to be unchanged. However, if I add GetData() method to a button, the property changes and data is displayed in the console as intended. All sources that I looked through suggest that the right way to load the DB if I need it right away is to use ngOnInit() hook, that's why I don't

send multiple async post request with tornado

匿名 (未验证) 提交于 2019-12-03 10:09:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: there are several questions on stackoverflow regarding tornado I still haven't found out an answer to my question I have a big text file that I wish to iterate on and send each line as a POST http request. I wish to do it async ( I need it to be fast) and then check the responses of the requests. I have something like that http_client = httpclient . AsyncHTTPClient () with open ( filename ) as log_file : for line in log_file : request = httpclient . HTTPRequest ( self . destination , method = "POST" , headers = self . headers ,

async/wait

青春壹個敷衍的年華 提交于 2019-12-03 10:05:12
原理 :将 Generator 函数和自动执行器,包装在一个函数里。 1 async function fn(args) { 2 // ... 3 } 4 5 // 等同于 6 7 function fn(args) { 8 return spawn(function* () { 9 // ... 10 }); 11 } 与其他异步方式比较 :Async 函数的实现最简洁,最符合语义,几乎没有语义不相关的代码。它将 Generator 写法中的自动执行器,改在语言层面提供,不暴露给用户,因此代码量最少。如果使用 Generator 写法,自动执行器需要用户自己提供。 async 函数对 Generator 函数的改进 : (1)内置执行器。 Generator 函数的执行必须靠执行器,所以才有了 co 模块,而 async 函数自带执行器。也就是说, async 函数的执行,与普通函数一模一样,只要一行。 asyncReadFile(); 上面的代码调用了 asyncReadFile 函数,然后它就会自动执行,输出最后结果。这完全不像 Generator 函数,需要调用 next 方法,或者用 co 模块,才能真正执行,得到最后结果。 (2)更好的语义。 async 和 await ,比起星号和 yield ,语义更清楚了。 async 表示函数里有异步操作, await

Should I await a 'async Task' function if I don't care its return value? [duplicate]

匿名 (未验证) 提交于 2019-12-03 09:18:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: This question already has an answer here: Do I have to await an async method? 2 answers Fire-and-forget with async vs “old async delegate” 4 answers What is the purpose of “return await” in C#? 6 answers C# Asynchronous call “invoke and forget” 3 answers Do I need to await a async Task function if it doesn't return anything? Will that cause the following code be wrapped in a delegate and executed after the async Task function returns? Task DoSomethingAsync () { return Task . Run (() => { // Do something, but doesn't return values.

How to modify a struct with async callbacks?

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to update a struct with multi-level nested async callback, Since each level callback provides info for next batch of requests till everything is done. It's like a tree structure. And each time I can only get to one level below. However, the first attempt with inout parameter failed. I now learned the reason, thanks to great answers here: Inout parameter in async callback does not work as expected My quest is still there to be solved. The only way I can think of is to store the value to a local file or persistent store and modify