[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() { 或者$( ...
随机推荐
- 互联网+时代IT管理者的转型
最近,大众创业万众创新的热潮真是一浪接着一浪,它实际上是一次政府和企事业的自我改革,利用互联网+的思维与技术对生产模式.流通模式与运营模式进行全新的变革,商业的本质是没有变的,仅仅是穿了个马甲来表演. ...
- 结构体类型重声明导致的bug一个
bug前提条件 当模块比較多.头文件较多,某个结构体类型会在当前模块中又一次声明进而引用其成员,而不直接包括其它模块的头文件. 这种优点是不引入不须要的类型声明到此模块.头文件包括的交叉:坏处是,添加 ...
- js ---- 实现千位分隔符
第一种方法: var num = 1234567; var string = num.toString(); var arr = string.split('').reverse(); console ...
- Catch Me If You ... Can't Do Otherwise--转载
原文地址:https://dzone.com/articles/catch-me-if-you-cant-do-otherwise I don't know whether it's an anti- ...
- maven仓库快速镜像
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- jquery模拟可输入的下拉框
//页面html <div id="select" class="select" > <ul> <c:forEach items= ...
- actionbar spinner-用法实例
今天需要更改一个actionbar上的spinner的字体颜色,结果试了好长时间都没有解决,最后才发现,原来他是在代码下增加的一个textview,然后使用adapter加载的,并不是直接用frame ...
- css中的!important作用
css中的!important作用 一.总结 1.!important:是hack, 2.!important作用:让浏览器首选执行这个语句,当对同一个对象设置了多个同类型的属性的时候,首选执行这一个 ...
- Vue 执行npm run dev时报了三个warning的原因
刚装上了win10,用vue运行下项目,输入npm run dev之后报了三个以下错误: There are multiple modules with names that only differ ...
- Linux学习总结(4)——Centos6.5使用yum安装mysql——快速上手必备
第1步.yum安装mysql [root@stonex ~]# yum -y install mysql-server 安装结果: Installed: mysql-server.x86_6 ...