[ES7] Convert Any Function into an Asynchronous Function
Any function can be made asynchronous, including function expressions, arrow functions, and methods. This lesson shows the syntax for each of the function types.
For example, we have a demo:
const fetch = require('node-fetch');
const BASE_URL = 'https://api.github.com/users';
const fetchGitHubUser = async (handle) => {
    const response = await fetch(`${BASE_URL}/${handle}`);
    return await response.json();
};
fetchGitHubUser('zhentian-wan')
    .then(console.log)
Since 'fetchGithubUser' is also an async function, we can convert it to async/await function:
const fetch = require('node-fetch');
const BASE_URL = 'https://api.github.com/users';
const fetchGitHubUser = async (handle) => {
    const response = await fetch(`${BASE_URL}/${handle}`);
    return await response.json();
};
(async () => {
    const user = await fetchGitHubUser('zhentian-wan');
    console.log(user);
})();
Here we must wrap await function inside IIFE async function, otherwise it won't work.
We can also convert 'fetchGithubUser' function into a class:
const fetch = require('node-fetch');
const BASE_URL = 'https://api.github.com/users';
class GithubUser {
    async fetchGitHubUser(handle) {
        const response = await fetch(`${BASE_URL}/${handle}`);
        return await response.json();
    }
}
(async () => {
    const github = new GithubUser();
    const user = await github.fetchGitHubUser('zhentian-wan');
    console.log(user);
})();
[ES7] Convert Any Function into an Asynchronous Function的更多相关文章
- AsyncCalls – Asynchronous function calls
		AsyncCalls – Asynchronous function callsWith AsyncCalls you can execute multiple functions at the sa ... 
- How can I create an Asynchronous function in Javascript?
		哈哈:)我的codepen 的代码笔记是:http://codepen.io/shinewaker/pen/eBwPxJ --------------------------------------- ... 
- DeprecationWarning: Calling an asynchronous function without callback is deprecated. - how to find where the “function:” is?
		I recently updated my node to 7.2.1 and noticed that there is a warning coming: (node:4346) Deprecat ... 
- $(window).load(function() {})和$(document).ready(function(){})的区别
		JavaScript 中的以下代码 : Window.onload = function (){// 代码 } 等价于 Jquery 代码如下: $(window).load(function ( ... 
- JQuery $(function(){})和$(document).ready(function(){})
		document.ready和onload的区别——JavaScript文档加载完成事件页面加载完成有两种事件一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件)二是onload,指 ... 
- $(function(){})和$(document).ready(function(){})  的用法
		当文档载入完毕就执行,以下几种效果是等价的:1. $(function(){ //这个就是jQuery ready()的简写,即下2的简写 // do something }); 2. $(docum ... 
- $(function(){})和$(document).ready(function(){})
		document.ready和onload的区别——JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ... 
- $(function(){})与$(document).ready(function(){})
		$(function(){ //jq ready()的简写 }); $(document).ready(function(){ // }); 或者: $().ready(function(){ //j ... 
- $(window).on("load",function(){} 和 $(document).ready(function() {}
		$(window).on("load",function(){ //页面属性,图片,内容完全加载完,执行 } $(document).ready(function() { 或者$( ... 
随机推荐
- 【UML】UML在软件开发各个阶段的应用
			一.UML5个互联视图 UML中经常使用5个互联的视图来描写叙述系统的体系结构. 如图 (1)用例视图(Use-case View) 由专门描写叙述可被终于用户.分析人员.測试人员看到的系统行为的用例 ... 
- cluster discovery概述及FaultDetection分析
			elasticsearch cluster实现了自己发现机制zen.Discovery功能主要包括以下几部分内容:master选举,master错误探测,集群中其它节点探测,单播多播ping.本篇会首 ... 
- 44.delete用法
			声明+delete:函数禁止使用.可以使一个类禁止释放 
- mahout历史(二)
			mahout历史 Apache Mahout起源于2008年,经过两年的发展,2010年4月ApacheMahout最终成为了Apache的顶级项目.Mahout 项目是由 ApacheLucene( ... 
- golang webservice[ json Martini webframe]
			golang webservice[ json Martini webframe] https://github.com/brunoga/go-webservice-sample 自己修改了一下例子, ... 
- oracle数据库spfile
			http://blog.chinaunix.net/uid-8996530-id-3195808.html http://www.cnblogs.com/HondaHsu/p/4885318.html ... 
- Linux下守护进程精析
			什么是守护进程? 守护进程就是通常所说的Daemon进程,它是Linux中的后台服务程序. 它是一个生存期较长的进程,通常独立于终端而且周期性的运行某种须要的任务以及有时候会等待一些将会发生的 ... 
- theme-windowAnimationStyle 动画四个方法的意义
			首先看代码 <style name="Animation.Activity"> <!--A打开B,B的出现动画--> <item name=" ... 
- php编译参数注释
			1. 指定安装路径 --prefix=PREFIX 2. 指定运行用户 --with-fpm-user=nginx 3. 指定运行组 --with-fpm-group=nginx 3.与'--pref ... 
- 1.1 Introduction中  Consumers官网剖析(博主推荐)
			不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Consumers 消费者(Consumers) Consumers label t ... 
