[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() { 或者$( ...
随机推荐
- Android开发经验之获取画在画布上的字符串长度、宽度(所占像素宽度)
Android中获取字符串长度.宽度(所占像素宽度) 计算出当前绘制出来的字符串有多宽,可以这么来! 方法1: Paint paint = new Paint(); Rect rect = new R ...
- 使用Python开发轻量级的Web框架以及基于WSGI的服务器来实现一个网站页面
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目说明 二丶数据准备 三丶使用网络TCP开发一个基于WSGI协议的Web服务器 四丶使用python3开发一个轻量级的 ...
- 3.阿里巴巴dubbo分布式rpc框架详细使用教程
dubbo是阿里巴巴开源的分布式服务框架,致力于提供高性能和透明化的rpc远程服务调用方案,以及soa服务治理方案,如果没有分布式需求,是不需要dubbo的,分布式环境dubbo的使用架构官方给出了一 ...
- 解决Cookie乱码
在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因 为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cookie时,先将 ...
- IOS的UIWebView中JS点击事件,需要加入cursor:pointer;属性才可以
IOS的UIWebView中JS点击事件,需要加入cursor:pointer;属性才可以. Android的WebView可以支持外链样式,js文件:IOS则需要改为内嵌样式和JS文件.
- axios封装http请求
import axios from 'axios' const HTTP_TIMEOUT = 15000; export function httpPost(url, params = {},head ...
- shader 3 rendering path
渲染通道, rendering path. vertexlit, forward 和 Deferred lighting 旧有的非统一架构下: 分为顶点着色引擎和像素渲染通道 渲染通道是GPU负责给图 ...
- Oracle批量插入在C#中的应用
public void SetUserReportResult(int[] reportId, bool isReceive, string result) { if (reportId == nul ...
- C语言深度剖析-----函数
认清函数的真面目 函数的意义 面向过程的程序设计 函数声明和定义 函数参数 编写代码的时候,不要编写类似先后调用的代码 f(k,k++) C语言中的顺序点 a--&&a ,& ...
- OC学习篇之—写类别(类的扩展)
首先我们来看一下场景,如果我们现在想对一个类进行功能的扩充,我们该怎么做? 对于面向对象编程的话,首先会想到继承,但是继承有两个问题: 第一个问题:继承的前提是这个类可以被继承,因为在Java中有些类 ...