Introduction

Do you prefer the usage of "ES6 Promise"? If you do, you will like the usage of "Fetch" too.

Compared to "Ajax", "Fetch" owns a competitive feature: promise, which synchronize asynchronous methods elegantly, the meaning and the usage of "Fetch" can be understood easily as well.

Here, I'd like to list the most common usage of "Fetch".

Flow

The flow of fetching staff is simple:

Usage

Fetch once

Suppose we would fetch the content of an remote html

fetch('./data/test.html')
.then(function (response) {
return response.text() // return a promise
})
.then(function (body) {
console.log( body ) // log: html content
})

Fetch data right after the other data fetched(Chain)

If we'd like to fetch data(json) right after fetching content(html)

fetch('./data/test.html')
.then(response => {
return response.text()
})
.then(body => {
console.log(body)
return fetch('./data/test.json') // return a promise(`fetch('/url')` will return a promise )
})
.then(response => {
return response.json() // return a promise too
})
.then(json => {
console.log(json) // log: json's data
})

Complete all fetching action

Promise.all([
Promise.resolve(fetch('./data/test.html')),
Promise.resolve(fetch('./data/test.json'))
]).then(data => {
console.log('Two requests are both completed!')
})

API

Github Fetch Document

Offcial Manual

Conclusion

Fetch, well done!

Gist - Fetch Usage的更多相关文章

  1. [转]Backbone.js简单入门范例

    本文转自:http://dmyz.org/archives/598 11年刚开始用前端MVC框架时写过一篇文章,当时Knockout和Backbone都在用,但之后的项目全是在用Backbone,主要 ...

  2. Backbone学习笔记一Backbone中的MVC

    原文章地址http://bigdots.github.io/2015/12/01/Backbone学习笔记(一)/#more Backbone.js为复杂WEB应用程序提供模型(models).集合( ...

  3. nutch2.3命令参数解析

    nutch中可执行的命令列表 [root@ewanalysis ~]# nutch Usage: nutch COMMAND where COMMAND is one of: inject injec ...

  4. Oracle Extended Tracing

      Definitions A trace file is a file that contains diagnostic data used to investigate problems. Als ...

  5. 【比较基因组】McScan jcvi比较两个基因组共线性细节记录

    目录 软件的安装 基因组的准备 一些细节 建议和示例 软件的安装 Python版McScan(jcvi工具包):https://github.com/tanghaibao/jcvi 以前只有pytho ...

  6. .Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”

    一.  问题说明 最近.Net EF core 程序部署到服务器,服务器数据库安装的是SQL server 2008 R2,我本地用的的是SQL server 2014,在用到分页查询时报错如下: H ...

  7. .NET Core EF框架使用SQL server 2008数据库分页问题:Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement

    一. 问题 最近.Net Core程序部署到服务器,采用EF6.本地数据库是SQL server 2016,服务器数据库安装的是SQL server 2008 R2,在用到分页查询时报错如下: { & ...

  8. usage: git remote add [<options>] <name> <url> -f, --fetch fetch the remote branches --tags import all tags and associated objects when fetching

    按照git官网提示输入 git pushgit remote add origin git@github.com:***3 / elm-1.git -u 链接git远程仓库 出现错误 usage: g ...

  9. 升级 nop 4.1 Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.

    Incorrect syntax near 'OFFSET'.  Invalid usage of the option NEXT in the FETCH statement. nop.web 项目 ...

随机推荐

  1. Android官方架构组件介绍之LifeCycle

    Google 2017 I/O开发者大会于近日召开,在开发者大会上谷歌除了发布了Android O等一些新产品之外,也对Android代码的架构做出了一个官方的回应. Google 2017 I/O开 ...

  2. Thread in Java

    References: [1]. http://www.javaworld.com/article/2074481/java-concurrency/java-101--understanding-j ...

  3. 浏览器兼容性--new Date

    ie浏览器下new Date("2013/04")与new Date("2016-04")会报错: //将201601格式的字符串转为Date对象,月份从0开始 ...

  4. iOS UIAlertView 文字对其方式 文字大小 设置方法

    - (void) willPresentAlertView:(UIAlertView *)alertView { for (UIView *subViewin alertView.subviews) ...

  5. UIWebView 跳过HTTPS证书认证

    UIWebView跳过证书认证 在UIWebView中加入如下代码即可(Error Domain=NSURLErrorDomain Code=-1202) //跳过证书验证 @interface NS ...

  6. Windows下以Local模式调试SparkStreaming的WordCount例子

    1.下载Windows版的NetCat https://eternallybored.org/misc/netcat/ 2.启动NetCat nc -l -p 9999 3.将SAPRK_HOME\c ...

  7. 初码-Azure系列-记一次MySQL数据库向Azure的迁移

    初码Azure系列文章目录 还在继续给客户迁移不同的系统到Azure,这一次是一个系统的MySQL数据库要迁移,将迁移过程记录一下 原系统环境 数据库版本:MySQL Community Editio ...

  8. 这是假的JS——利用CSS Animation实现banner图非交互循环播放

    话不多说,先来张html和css代码截图~ 注意事项: 1.如果想在每张图前进行停顿,可以在keyframes中的每一步前加上一小段与下一张相同的代码: 2.如果想要在实现无违和感的最后一张与第一张的 ...

  9. selinux导致docker启动失败

    1. 问题描述:一向运行正常的一群容器,突然有一天挂掉了,再也起不来,报错如下 Error response from daemon: devmapper: Error mounting '/dev/ ...

  10. dedecms搜索提示"关键字不能小于2个字节!"

    在测试自己制作的搜索页模板时,如果遇到搜索时提示"关键字不能小于2个字节!"!打开plus/search.php把   if(($keyword=='' || strlen($keyword)< ...