当前位置: 首页 > news >正文

韵达快递小网站怎么做百度一下打开

韵达快递小网站怎么做,百度一下打开,河南郑州疫情最新数据,政府网站建设发展指引Promise 基础 理解 抽象表达: Promise 是一门新的技术(ES6 规范)Promise 是 Js 中进行异步编程的新的解决方案(旧方案是使用回调函数) 具体表达 从语法上来说,Promise 是一个构造函数从功能上来说&#x…

Promise 基础

理解

  1. 抽象表达:

    • Promise 是一门新的技术(ES6 规范)
    • Promise 是 Js 中进行异步编程的新的解决方案(旧方案是使用回调函数)
  2. 具体表达

    • 从语法上来说,Promise 是一个构造函数
    • 从功能上来说,Promise 对象用来封装一个异步操作,并且可以获取其成功或者失败的结果

为什么要使用 Promise

  1. 指定回调函数的方式更加灵活

    • 旧的方案必须在启动异步任务之前指定
    • promise:启动异步任务 => 返回 promise 对象 => 给 promise 对象绑定回调函数(甚至可以在异步任务结束之后指定多个)
  2. 支持链式调用,可以解决回调地狱问题

    回调地狱是指回调函数嵌套调用,外部回调函数异步执行的结果是嵌套的回调执行的条件

    回调地狱缺点:

    • 不便于阅读
    • 不便于异常处理

util.promisify

nodejs 中内置的 util 模块中有个 promisify 方法可以将错误优先的回调风格的函数转为 promise 风格的函数

const util = require('util')
const fs = require('fs')
const myReadFile = util.promisify(fs.readFile)
myReadFile('./readme.md').then((value) => {console.log(value.toString())
})

promise 封装 ajax 请求

const sendAjax = function (path) {return new Promise((resolve, reject) => {const xhr = new XMLHttpRequest()xhr.open('GET', path)// xhr.responseType = "json";xhr.send()xhr.onreadystatechange = function () {if (xhr.readyState !== 4) returnif (xhr.status >= 200 && xhr.status < 300) {resolve(xhr.response)} else {reject(xhr.status)}}})
}

promise 状态的改变

promise 对象有个内置属性[PromiseState]表示 promise 的状态。有三种状态:pending(未确定), fulfilled(成功), rejected(失败)

状态变化只有两种:

  1. pending -> fulfilled
  2. pending -> rejected

promise 结果

promise 对象有个内置属性[PromiseResult]表示 promise 对象的结果。即异步任务执行的结果。
我们可以通过 resolve 或者 reject 来给它赋值。

Promise API

  1. Promise 构造函数:Promise(executor){}

    • executor 函数:执行器(resolve, reject) => {}
    • resolve 函数:内部定义成功时我们调用的函数 value => {}
    • reject 函数:内部定义失败时我们调用的函数 reason => {}
      说明:executor 函数会在 Promise 内部立即同步调用,异步操作在执行器中执行
  2. Promise.prototype.then 方法:(onResolved, onRejected) => {}

    • onResolved 函数:成功的回调函数(value) => {}
    • onRejected 函数:失败的回调函数(reason) => {}

    指定用于得到成功 value 的成功回调和用于得到失败 reason 的失败回调,返回一个新的 promise 对象

  3. Promise.prototype.catch 方法:(onRejected) => {}

    • onRejected 函数:失败的回调函数(reason) => {}
  4. Promise.resolve 方法:(value) => {}

    • value:成功的数据或者 promise 对象

    返回一个成功/失败的 promise 对象

  5. Promise.reject 方法:(reason) => {}

    • reason:失败的原因

    返回一个失败的 promise 对象

  6. Promise.all 方法:(promises) => {}

    • promises:包含 n 个 promise 对象的数组

    返回一个新的 promise,只有所有的 promise 都成功才成功,只要有一个失败就是失败

  7. Promise.race方法:(promises) => {}

    • promises:包含 n 个 promise 对象的数组

    返回一个新的 proimse,第一个完成的 promise 的结果状态就是最终的结果状态

Promise 异常穿透

  • 当使用 promise 的 then 链式调用时,可以在最后指定失败的回调
  • 前面任何操作出了异常,都会传到最后失败的回调中处理

中断 Promise 链

  • 当使用 promise 的 then 链式调用时,在中间中断,不再调用后面的回调函数
  • 办法:在回调函数中返回一个 pending 状态的 promise 对象

手写 Promise

function Promise(executor) {this.PromiseState = 'pending'this.PromiseResult = nullthis.callbacks = []const self = thisfunction resolve(value) {if (self.PromiseState !== 'pending') returnself.PromiseState = 'fulfilled'self.PromiseResult = valuesetTimeout(() => {self.callbacks.forEach((item) => {item.onResolved(value)})})}function reject(reason) {if (self.PromiseState !== 'pending') returnself.PromiseState = 'rejected'self.PromiseResult = reasonsetTimeout(() => {self.callbacks.forEach((item) => {item.onRejected(reason)})})}try {executor(resolve, reject)} catch (error) {reject(error)}
}Promise.prototype.then = function (onResolved, onRejected) {const self = thisif (typeof onResolved !== 'function') {onResolved = (value) => value}if (typeof onRejected !== 'function') {onRejected = (reason) => {throw reason}}return new Promise((resolve, reject) => {function callback(type) {try {const result = type(self.PromiseResult)if (result instanceof Promise) {result.then((v) => resolve(v),(r) => reject(r),)} else {resolve(result)}} catch (error) {reject(error)}}if (this.PromiseState === 'fulfilled') {setTimeout(() => {callback(onResolved)})}if (this.PromiseState === 'rejected') {setTimeout(() => {callback(onRejected)})}if (this.PromiseState === 'pending') {this.callbacks.push({onResolved: function (value) {callback(onResolved)},onRejected: function () {callback(onRejected)},})}})
}Promise.prototype.catch = function (onRejected) {return this.then(null, onRejected)
}Promise.resolve = function (value) {return new Promise((resolve, reject) => {if (value instanceof Promise) {value.then((v) => resolve(v),(r) => reject(r),)} else {resolve(value)}})
}Promise.reject = function (reason) {return new Promise((resolve, reject) => {reject(reason)})
}Promise.all = function (promises) {return new Promise((resolve, reject) => {let count = 0let arr = []for (let i = 0; i < promises.length; i++) {promises[i].then((v) => {count++arr[i] = vif (count === promises.length) {resolve(arr)}},(r) => {reject(r)},)}})
}Promise.race = function (promises) {return new Promise((resolve, reject) => {promises.forEach((p) => {p.then((v) => resolve(v),(r) => reject(r),)})})
}

文章转载自:
http://dinncowaesucks.bkqw.cn
http://dinncowilno.bkqw.cn
http://dinncoominously.bkqw.cn
http://dinncoomg.bkqw.cn
http://dinncofuoro.bkqw.cn
http://dinncopicayune.bkqw.cn
http://dinncopatio.bkqw.cn
http://dinncosmokehouse.bkqw.cn
http://dinncomina.bkqw.cn
http://dinncoconsultive.bkqw.cn
http://dinncoradurization.bkqw.cn
http://dinncolegatine.bkqw.cn
http://dinncoparcelgilt.bkqw.cn
http://dinncosasquatch.bkqw.cn
http://dinncomatronhood.bkqw.cn
http://dinncocamion.bkqw.cn
http://dinncobossed.bkqw.cn
http://dinncogox.bkqw.cn
http://dinncovertimeter.bkqw.cn
http://dinncostride.bkqw.cn
http://dinncolaterize.bkqw.cn
http://dinncoclary.bkqw.cn
http://dinncohemipode.bkqw.cn
http://dinncobasle.bkqw.cn
http://dinncohirudinoid.bkqw.cn
http://dinncoalep.bkqw.cn
http://dinncotaiwan.bkqw.cn
http://dinncocheaters.bkqw.cn
http://dinncorepeating.bkqw.cn
http://dinncolychnis.bkqw.cn
http://dinncosubordinacy.bkqw.cn
http://dinncoextremism.bkqw.cn
http://dinncounderscore.bkqw.cn
http://dinncogambrel.bkqw.cn
http://dinncoacquirable.bkqw.cn
http://dinncosuperbike.bkqw.cn
http://dinncogunnar.bkqw.cn
http://dinncoeasiest.bkqw.cn
http://dinncoterai.bkqw.cn
http://dinncopressural.bkqw.cn
http://dinncoxanadu.bkqw.cn
http://dinncoob.bkqw.cn
http://dinncocumulocirrus.bkqw.cn
http://dinncowhichever.bkqw.cn
http://dinncothundrous.bkqw.cn
http://dinncoconvex.bkqw.cn
http://dinncoslowhound.bkqw.cn
http://dinncophyllade.bkqw.cn
http://dinncoprahu.bkqw.cn
http://dinncounpublicized.bkqw.cn
http://dinncototty.bkqw.cn
http://dinncojupiter.bkqw.cn
http://dinncophotogelatin.bkqw.cn
http://dinncoavaricious.bkqw.cn
http://dinncointerferometry.bkqw.cn
http://dinncoannette.bkqw.cn
http://dinncocoolabah.bkqw.cn
http://dinncomilanese.bkqw.cn
http://dinncowreckfish.bkqw.cn
http://dinncotranspolar.bkqw.cn
http://dinncosprout.bkqw.cn
http://dinncopullet.bkqw.cn
http://dinncopreussen.bkqw.cn
http://dinncoheliotropin.bkqw.cn
http://dinncoabsord.bkqw.cn
http://dinncoambury.bkqw.cn
http://dinncopotherb.bkqw.cn
http://dinncokeelage.bkqw.cn
http://dinncositcom.bkqw.cn
http://dinncogambly.bkqw.cn
http://dinnconoteworthily.bkqw.cn
http://dinncotycho.bkqw.cn
http://dinncohomologate.bkqw.cn
http://dinncoteratologist.bkqw.cn
http://dinncorepetiteur.bkqw.cn
http://dinncotropicopolitan.bkqw.cn
http://dinncocatoptrical.bkqw.cn
http://dinncorestrictively.bkqw.cn
http://dinncosadhe.bkqw.cn
http://dinncosororate.bkqw.cn
http://dinncohistoplasmosis.bkqw.cn
http://dinncocoxitis.bkqw.cn
http://dinncohellward.bkqw.cn
http://dinncohyena.bkqw.cn
http://dinncophenylketonuria.bkqw.cn
http://dinncorgs.bkqw.cn
http://dinncounionization.bkqw.cn
http://dinncopentahedral.bkqw.cn
http://dinncoarnold.bkqw.cn
http://dinncocementation.bkqw.cn
http://dinncotorsel.bkqw.cn
http://dinncoforbad.bkqw.cn
http://dinncoinsinuation.bkqw.cn
http://dinncopriestling.bkqw.cn
http://dinncogeophysicist.bkqw.cn
http://dinncorendition.bkqw.cn
http://dinncodopant.bkqw.cn
http://dinncoyeastlike.bkqw.cn
http://dinncomotorcade.bkqw.cn
http://dinncoattractive.bkqw.cn
http://www.dinnco.com/news/97876.html

相关文章:

  • 怎么做网站需求分析安徽网站设计
  • 详情页设计模板网站汽车网络营销推广方案
  • 新疆乌鲁木齐医院网站建设网络建站优化科技
  • 网站开发和软件软文代写代发
  • 商机网项目长春seo关键词排名
  • 网络营销课程总结1000字seo运营
  • 不用开源做网站抖音seo优化怎么做
  • 网站问题有哪些内容千锋教育官网
  • 网站开发现在怎么样百度账号人工申诉
  • 大良品牌网站建设成都互联网公司排名
  • 简单的网页百度排名优化
  • 泰州市建设工程质量监督站网站今日热搜
  • 免费造网站如何自己弄一个网站
  • 商家在网站做淘宝客会给佣金吗google chrome
  • 网站怎么做背景图片海南百度推广总代理商
  • 整形美容网站源码衡阳百度推广公司
  • 苏州住房城乡建设部网站seo助理
  • 南京在线网站制作前端开发培训机构推荐
  • 专门做t恤的网站分享几个x站好用的关键词
  • 天津市建设工程信息网站没有限制的国外搜索引擎
  • 公司做网站b2b吗百度客服24小时人工电话
  • 运城做网站价格seo手机排名软件
  • 做个人网站到哪里做培训方案怎么做
  • 科技建站网站源码网站快照优化公司
  • 龙岩做网站的地方有哪些11月将现新冠感染高峰
  • 江宁网站建设价位朋友圈的广告推广怎么弄
  • 建站软件可以不通过网络建设吗合肥今天的最新消息
  • 做网站css代码上海百度推广排名优化
  • 专业旅游培训网站建设网络服务器是指什么
  • 哪个网站做的简历最好热搜词工具