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. crmjs区分窗口是否是高速编辑

    有时候,我们须要区分打开的窗口是否是高速编辑页面,在上面做一些逻辑处理: 窗口上面附加的js代码: function loadFrom() {     var formType = Xrm.Page. ...

  2. GetListToJson

    List<Models.ArticleModel> list = GetList(page);    return Newtonsoft.Json.JsonConvert.Serializ ...

  3. NET Framework 4.5 有更加简便的方法 Task.Run()

    NET Framework 4.5 有更加简便的方法 Task.Run()

  4. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

  5. webstorm快捷键(觉得有用,喜欢的话可以保存收藏哦)

    Ctrl+/ 或 Ctrl+Shift+/------------------------->>注释(// 或者/*…*/ ) Ctrl+X删除行 Ctrl+D复制行 Ctrl+G查找行 ...

  6. atxserver2安装与使用

    atxserver2的使用 1.首先clone atxserver2代码,此时使用pip3 install requirements后执行python main.py 会提示“ [WinError 1 ...

  7. JS实践与写博客-序

    大二的时候,就开始接触JavaScript了. 当时学了1年多,主要是认真看了一本JavaScript的入门书籍,了解了JavaScript大致怎么回事.在独自做Web项目的时候,用的都是JavaSc ...

  8. Servlet 规范笔记—基于http协议的servlet

    在上一章节,我们大概的描述了servlet的规范以及servlet和servlet容器的概念和用途,我们清楚的知道servlet容器提供了接收来自client端的请求,然后根据请求进行处理(如:执行对 ...

  9. (JavaScript基础向)sort()方法里的排序函数的理解

    比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...

  10. 添加asp.net mvc到现有的asp.net web form 应用程序

    前言 asp.net mvc的前一版本为asp.net web Form(Asp.net mvc之前称为asp.net),其第一个版本与2002年年初发布.asp.net web form 属于.ne ...