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的更多相关文章

  1. AsyncCalls – Asynchronous function calls

    AsyncCalls – Asynchronous function callsWith AsyncCalls you can execute multiple functions at the sa ...

  2. How can I create an Asynchronous function in Javascript?

    哈哈:)我的codepen 的代码笔记是:http://codepen.io/shinewaker/pen/eBwPxJ --------------------------------------- ...

  3. 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 ...

  4. $(window).load(function() {})和$(document).ready(function(){})的区别

    JavaScript 中的以下代码 : Window.onload = function (){// 代码 }  等价于  Jquery 代码如下: $(window).load(function ( ...

  5. JQuery $(function(){})和$(document).ready(function(){})

    document.ready和onload的区别——JavaScript文档加载完成事件页面加载完成有两种事件一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件)二是onload,指 ...

  6. $(function(){})和$(document).ready(function(){}) 的用法

    当文档载入完毕就执行,以下几种效果是等价的:1. $(function(){ //这个就是jQuery ready()的简写,即下2的简写 // do something }); 2. $(docum ...

  7. $(function(){})和$(document).ready(function(){})

    document.ready和onload的区别——JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ...

  8. $(function(){})与$(document).ready(function(){})

    $(function(){ //jq ready()的简写 }); $(document).ready(function(){ // }); 或者: $().ready(function(){ //j ...

  9. $(window).on("load",function(){} 和 $(document).ready(function() {}

    $(window).on("load",function(){ //页面属性,图片,内容完全加载完,执行 } $(document).ready(function() { 或者$( ...

随机推荐

  1. hash_set和hash_map

    1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...

  2. JNI/NDK开发指南(九)——JNI调用性能測试及优化

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/44279725 在前面几章我们学习到了.在Java中声明一个native方法,然后生成本地接 ...

  3. 36.Node.js 工具模块--OS模块系统操作

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js os 模块提供了一些基本的系统操作函数.我们可以通过以下方式引入该模 ...

  4. golang sync.WaitGroup

    //阻塞,直到WaitGroup中的所以过程完成. import ( "fmt" "sync" ) func wgProcess(wg *sync.WaitGr ...

  5. node 内存溢出

    遇到这个问题的人可以更快解决 再复制写一篇 利于百度搜索 坑爹的node 内存溢出 react开发项目  安装一个插件依赖 ,然后就报错了 报错如下(自己的没有截图出来 这是从别人的截图---报错基本 ...

  6. Appium_pytest fixture的使用

    一.定义fixture方法 # -*- coding:utf-8 -*-import pytestfrom baseutil.DriverUtil import DriverConfig @pytes ...

  7. 3/18 Django框架 启动django服务

    web框架:本质是socket服务端,socket通常也被称为"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信.web框架就是将 ...

  8. PYTHON学习第五天课后总结:

    今日重点: 数字类型 字符串类型 列表类型 元组类型 1,数字类型    数字类型为不可变类型   也只能将纯数字类型的字符串转换成int包括: 整型: int()     int() 为内置函数,可 ...

  9. hdu4605Magic Ball Game 树状数组

    //给一棵树.树的每个节点的子节点个数是0或2 //对于每个节点都有一个权值w[i] //一个权值为x的球在每个节点的情况有 //x=w[i] 这个球在该点不向下掉 //x<w[i] 这个球往左 ...

  10. SQL server 2012 安装SQL2012出现报错: 启用 Windows 功能 NetFx3 时出错

    在window server 2012服务器上,安装 SQL Server 2012的过程中,报了一个错误,一个安装失败, 在安装SQL 2012的过程中.出现下面错误:启用 Windows 功能 N ...