JavaScript Promise迷你书(中文版)
迷你书(中文版) 7 Promise简介 在 ES6 Promises 标准中定义的API还不是很多。 目前大致有下面三种类型。 Constructor Promise类似于 XMLHttpRequest ,从构造函数 Promise 来创建一个新建新 promise 对 象作为接口。 要想创建一个promise对象、可以使用 new 来调用 Promise 的构造器来进行实例化。 resolve(处理结果值) • 处理结果错误的话,调用 reject(Error对象) 按这个流程我们来实际编写下promise代码吧。 我们的任务是用Promise来通过异步处理方式来获取XMLHttpRequest(XHR)的数据。 创建XHR的promise对象 首先,创建一个用Promise把XHR处理包装起来的名为 getURL 的函数。 xhr-promise.js 15 https://speakerdeck function getURL(URL) { return new Promise(function (resolve, reject) { var req = new XMLHttpRequest(); req.open('GET', URL, true); req.onload = function () { if (req0 码力 | 112 页 | 1010.02 KB | 1 年前3JavaScript Promiseの本 v1
Overview ES6 Promisesの仕様で定義されているAPIはそこまで多くはありません。 大きく分けて以下の3種類になります。 Constructor Promiseは XMLHttpRequest のように、コンストラクタ関数である Promise からインスタ ンスとなる promiseオブジェクトを作成して利用します。 promiseオブジェクトを作成するには、 Promise resolve(結果の値) を呼ぶ • 処理結果がエラーなら、 reject(Errorオブジェクト) を呼ぶ この流れに沿っているものを実際に書いてみましょう。 非同期処理であるXMLHttpRequest(XHR)を使いデータを取得するものをPromiseで書 いていきます。 XHRのpromiseオブジェクトを作る まずは、XHRをPromiseを使って包んだような getURL function getURL(URL) { return new Promise(function (resolve, reject) { var req = new XMLHttpRequest(); req.open('GET', URL, true); req.onload = function () { if (req0 码力 | 115 页 | 1.06 MB | 1 年前3JavaScript Promiseの本 v2
Overview ES Promisesの仕様で定義されているAPIはそこまで多くはありません。 大きく分けて以下の3種類になります。 Constructor Promiseは XMLHttpRequest のように、コンストラクタ関数である Promise からインスタ ンスとなる promiseオブジェクトを作成して利用します。 promiseオブジェクトを作成するには、 Promise resolve(結果の値) を呼ぶ • 処理結果がエラーなら、 reject(Errorオブジェクト) を呼ぶ この流れに沿っているものを実際に書いてみましょう。 非同期処理であるXMLHttpRequest(XHR)を使いデータを取得するものをPromiseで書 いていきます。 XHRのpromiseオブジェクトを作る まずは、XHRをPromiseを使って包んだような fetchURL function fetchURL(URL) { return new Promise((resolve, reject) => { const req = new XMLHttpRequest(); req.open("GET", URL, true); req.onload = () => { if (200 <= req0 码力 | 137 页 | 1.17 MB | 1 年前3《Slides Dev Web》 06. HTTP & AJAX
dynamiques de formulaires, autocompletion – Validation avec interrogation du serveur – … L’objet XMLHttpRequest • Initiative de Microsoft – Composant ActiveX de IE5 – Adopté par Mozilla 1.0 et Safari 1.2 ss.com/Content/Development/JavaScript/Articl es/GIFAsPipe/Index.cfm 10https://www.w3.org/TR/XMLHttpRequest/ 4 • Supporté par la majorité des navigateurs • Alternative souhaitable si JS désactivé XHR ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } } • Dans son contexte11 XHR en jQuery avec load()0 码力 | 11 页 | 91.09 KB | 1 年前3Google Java Style Guide
almost entirely disregarded. Exam- ples: Prose form Correct Incorrect “XML HTTP request” XmlHttpRequest XMLHTTPRequest “new customer ID” newCustomerId newCustomerID “inner stopwatch” innerStopwatch innerStopWatch0 码力 | 19 页 | 84.76 KB | 1 年前3阮一峰 《ECMAScript 6入门》 第三版
function(url) { var promise = new Promise(function(resolve, reject){ var client = new XMLHttpRequest(); client.open("GET", url); client.onreadystatechange = handler; client.responseType log('Contents: ' + json); }, function(error) { console.error('出错了', error); }); 上面代码中, getJSON 是对 XMLHttpRequest 对象的封装,用于发出一个针对 JSON 数据的 HTTP 请求,并且返回一个 Promise 对象。需要注意的是, 在 getJSON 内部, resolve 函数和 reject 64位浮点数 double 注意,二进制数组并不是真正的数组,而是类似数组的对象。 很多浏览器操作的API,用到了二进制数组操作二进制数据,下面是其中的几个。 File API XMLHttpRequest Fetch API Canvas WebSockets ArrayBuffer 对象 概述 ArrayBuffer 对象代表储存二进制数据的一段内存,它不能直接读写,只能通过0 码力 | 679 页 | 2.66 MB | 1 年前3The Express Handbook
cookies sent by the request (needs the cookie-parser middleware) .xhr true if the request is an XMLHttpRequest 5. Send a response to the client In the Hello World example we used the send() method of0 码力 | 34 页 | 518.49 KB | 1 年前3Apache Unomi 1.1.x - Documentation
var url = 'http://localhost:8181/context.json?sessionId=' + sessionId; var xhr = new XMLHttpRequest(); var isGet = data.length < 100; if (isGet) { xhr.withCredentials = true;0 码力 | 32 页 | 477.20 KB | 1 年前3CakePHP Cookbook 2.x
is OPTIONS. is('ajax') Check to see whether the current request came with X-Requested- With = XMLHttpRequest. is('ssl') Check to see whether the request is via SSL is('flash') Check to see whether the request }); JsHelper::request($url, $options = array()) Generate a JavaScript snippet to create an XmlHttpRequest or ‘AJAX’ request. Event Options complete - Callback to fire on complete. success - Callback JsHelper::submit($caption = null, $options = array()) Create a submit input button that enables XmlHttpRequest submitted forms. Options can include both those for FormHelper::submit() and JsBaseEngine::request()0 码力 | 1096 页 | 958.62 KB | 1 年前3CakePHP Cookbook 2.x
OPTIONS. • is('ajax') Check to see whether the current request came with X-Requested-With = XMLHttpRequest. • is('ssl') Check to see whether the request is via SSL • is('flash') Check to see whether }); JsHelper::request($url, $options = array()) Generate a JavaScript snippet to create an XmlHttpRequest or ‘AJAX’ request. Event Options • complete - Callback to fire on complete. • success - Callback JsHelper::submit($caption = null, $options = array()) Create a submit input button that enables XmlHttpRequest submitted forms. Options can include both those for FormHelper::submit() and JsBaseEngine::request()0 码力 | 820 页 | 2.52 MB | 1 年前3
共 141 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15