Promise , fetch Api ,blueBird,Q,When

Promise 核心原理与工程实践:从状态机到生产级封装 Promise 是 JavaScript 中用于抽象异步操作的核心机制,其本质是一个遵循 pending/fulfilled/rejected 三态不可逆演化的状态机。它通过微任务队列实现确定性执行时序,解决了回调地狱、错误丢失和控制流断裂等经典问题。作为 ES6(ES2015)正式纳入语言标准的原生能力,Promise 不仅支撑 async/await 语法糖,更是 Suspense、Web Workers 跨线程通信及 TypeScript 类型推导的底层契约基础。本文深入解析 Promise 的执行器 阅读详情

Promise which the solution of async programmer is the original obj presented by ecmaScript 6 , it’s better than the asynchronous callback was presented by community ;

Promise is a object that can get asynchronous message, then method of then return the promise obj but different from the original promise obj

the second argument of then method is the same to .catch(); but the catch is optimized.

code:

let Promise = new Promise(function(res,rej){
    res("suc");

}).then( res=>{

    console.log(`the async of network's result is ${res}`);

}).catch((err)=>{

    console.log(err);

});


Below the code and the role of the same above


let Promise = new Promise((res,rej)=>{res("suc");})
.then(
  res=>{console.log(`the async of network's result is ${res}`);},
  err=>{console.log(err)}
);
The role of the resolve method is to bring the results out of the state of promise from pending to success,the role of the reject method is to bring the error results out of when the state of promise form pending to error.
let promise = new Promise(function(resolve, reject) {
  console.log('Promise');
  resolve();
});

promise.then(function() {
  console.log('Resolved.');
});

console.log('Hi!');

// Promise
// Hi!
// Resolved

// the output of Hi is before than Resolved because of Then method assign //callback that will execute after synchronous task 

core:

the es 6 promise have the same aim with bluebird to solve the callback trap , but es6 is late appear than blue bird
below is use promise , async , and normal way deal with callback issue

<script>
//refer : http://www.tuicool.com/articles/bUNj6ji
// blue bird provide promise api
// you can use es6 promise solve the callback
// you can use generator
//todo:// before es 6 Q,when.js,bluebird solve the callback use base promise 
    export default{
        data(){
            return{
                msg:'hello vue',
                url:"",
                url1:"",
            }
        },
        mounted:function(){
          this.getDataByBlueBird();

          window.p = pp;

        },


        methods:{

            getDataByPromise(){
                //request by promise
               this.$http.get(this.url).then(res=>{
                //deal with the first request
                console.log(res);
                return this.$http.get(`${this.url1}?msg=${res.data.msg}`)
              }).then(res=>{
                //deal with the second request
                console.log(res);
            }).catch(err=>{
                //deal with the error
                console.log(err);
            })

            },

            getDataByNormal(){
            // this.$http.get('').then(res=>{console.log()}).catch(err=>{console.log(err.statusText)})
           // vue promise and request intercept
            this.$http.get(this.url).then(res=>{
                  console.log(res);
                  //todo:// the next http
                  this.$http.get(this.url1).then(res=>{
                      console.log(res)
                  })
            })
            //catch the error all include inner error
            .catch(err=>{console.log(err.statusText)});

             // the callback trap isn't resolve by v-resource but promise just as blue bird

            },

            // the promise.all can't sure the result by httpRequest order
            getDataByPromiseAll(){

                const promises = [this.url,this.url1].map(item=>{
                      //todo://  return promise object the response order same to request order
                      return this.$http.get(item);

                });

                Promise.all(promises).then(res=>{
                    //todo:// the result is response data array
                    console.log(res);

                })
            },

            getDataByBlueBird(){
                  // return a promise obj you can .then the first argument of then is the last request res
                  // todo:// make every callback become a alone chunk
                  Promise.resolve()
                  .then(()=>{
                      return this.$http.get(this.url);
                  }).then(res =>{
                        console.log(res);
                        return this.$http.get(this.url1);
                  }).then(res =>{
                      console.log(res);
                  });

            },

            //async and await is better the generator , async like * await like yield
            //async function return promise obj , you can use then add callback . the function will wait when
            //execute await
            getDataByAsync(){
                let getData = async ()=>{
                     //todo:// if not use ()=> this will be undefined because ...
                     var f1 =  await this.$http.get(this.url);
                     var f2 = await this.$http.get(`${this.url1}token=${f1.data.code}`);
                      console.log(f1.data);
                      console.log(f2.data);
                }

                getData();

            },


        },

        components:{
        }
    }
async/await 实战指南:从语法糖到异步工程化 async/await 是 JavaScript 中将异步操作语义同步化的关键机制,其本质是 V8 引擎对 Promise 状态机的原生封装,而非简单语法糖。它通过‘暂停-恢复’执行模型,显著提升异步代码的可读性、可调试性与错误定位能力。相比传统 Promise 链,它解耦了控制流、数据流与错误流,支持精细化错误处理(如分层 try/catch)、并发策略选型(allSettled/race)及中断控制(AbortController)。在真实 Web 开发中,它已成中后台系统默认范式,广泛应用于接口请求、 阅读详情

相关推荐

中科蓝讯Downloader配置实战:从软硬开关机到ANC降噪的完整指南

本文提供了中科蓝讯Downloader工具的详细配置实战指南。内容涵盖从基础的软硬开关机选择与配置,到核心的EQ音效调试、触摸按键与入耳检测优化,以及高阶的ANC降噪与电源管理设置。旨在帮助开发者快速掌握关键配置项,高效完成蓝牙耳机产品的功能调试与优化。

s4t5u6v7的博客 515

Bluebird 高性能揭秘

Bluebird 是一个广泛使用的 Promise 库,最早在 2013 年得到人们的关注。相比其他同等水平的 Promise 库,Bluebird 快了一百来倍。Bluebird 自始至终遵循着 JavaScript 优化的一些基本原则,所以才有这么好的性能。本文将会介绍其中最有价值的三个方面。 1. 函数中的对象分配最小化 对象分配(object allocation),尤其是函数

程序猿开发日志【学习永无止境】 1922

文件上传漏洞攻击与防范方法

文件上传漏洞攻击与防范方法 文件上传漏洞简介: 文件上传漏洞是web安全中经常用到的一种漏洞形式。是对数据与代码分离原则的一种攻击。上传漏洞顾名思义,就是攻击者上传了一个可执行文件如木马,病毒,恶意脚本,WebShell等到服务器执行,并最终获得网站控制权限的高危漏洞。 文件上传漏洞危害: 上传漏洞与SQL注入或 XSS相比 , 其风险更大 , 如果 Web应用程序存在上传漏洞 , 攻击者上传...

m0_38103658的博客 4万+

npm-bluebird使用

API 注意 时刻注意return; 使用Promise.promisify简化对test(val, function(err, result){})的处理; 尽量避免使用deferred object; .spread 用于简化处理返回值是数组的情况; 用es6取代其处理返回数组的时候注意添加.all(); .catch() 允许传入错误类型(error constructor)来...

weixin_30339457的博客 707

【Qt】Qt5.15在线安装避坑指南:从代理配置到组件选择的完整实践

本文详细介绍了Qt5.15在线安装的全流程避坑指南,包括代理配置、组件选择和常见问题解决方案。通过使用Fiddler工具和国内镜像源,可显著提升下载速度。文章还提供了组件选择的黄金法则和安装后的验证步骤,帮助开发者高效完成Qt5.15的安装与配置。

weixin_42550052的博客 276

单细胞双胞过滤-scrublet安装

pip install scrublet ----use-pep517有如下报错,分析可知安装annoy时不会自动更新gcc的版本,centos默认是4.8.5。pip install scrublet ----use-pep517 仍然报错,但似乎不影响使用,提示Successfully installed。打开终端,进入Python环境,输入以下代码,查看Python安装位置。暂时还没尝试是否存在问题,如果存在问题,降级scrublet版本。包的开发依赖的是比较低版本的numpy,会用到。

hzau_t的博客 803

利用promise写出更加优美的nodejs程序

利用promise写出更加优美的nodejs程序 时间 2014-04-28 10:42:17 sundaqing的个人空间 原文 http://my.oschina.net/sundq/blog/227410 主题 Node.js 什么是 promisepromise 一个标准,它描述了异步调用的返回结果,包括正确返回结果和错误处理。关于详细的说明文档可以参考 Promises/A+

一名普通码农的菜地 2197

Unity跨平台存储避坑指南:persistentDataPath权限真相

Application.persistentDataPath是Unity中用于持久化数据的核心路径,表面提供跨平台抽象,实则底层访问机制因操作系统而异。其原理并非简单路径映射,而是受Android Scoped Storage、iOS Data Protection、Windows组策略及macOS隐私权限等多层安全模型动态约束。技术价值在于保障用户数据长期可靠存储,但若忽略平台差异,极易引发‘本地正常、真机崩溃’‘iOS可存、Android报错’等线上事故。典型应用场景包括游戏存档、离线缓存、自定义资源包

weixin_32073537的博客 355

JavaScript Promise 异步编程全解析:从基础概念到实战避坑指南

异步编程是处理网络请求、文件读写等非阻塞操作的核心技术,旨在提升应用性能和响应能力。其核心原理在于通过事件循环和任务队列管理异步任务,避免主线程阻塞。Promise 作为 ES6 引入的异步编程管理范式,提供了标准化的解决方案,其技术价值在于通过状态机(Pending、Fulfilled、Rejected)和链式调用,优雅地解决了回调地狱问题,显著提升了代码可读性和可维护性。在应用场景上,Promise 广泛应用于前端 API 调用、Node.js I/O 操作以及任何需要处理异步流程的环节。本文深入剖析

weixin_34128839的博客 845

基于FPGA的多通道音频信号采样系统设计

FPGA是随着数字集成电路的发展而逐步演变而来的。从早期的电子管、晶体管、小规模集成电路,到超大规模集成电路(VLSIC),数字集成电路的集成度和性能不断提升。FPGA的出现为系统设计师提供了灵活的设计平台,使其能够在不依赖半导体厂商的情况下快速设计专用集成电路(ASIC)。FPGA具有设计周期短、成本低、开发工具先进等优点,广泛应用于原型设计和产品生产中。VHDL是一种用于描述数字系统的硬件描述语言,其语言形式类似于计算机高级语言,但包含了许多硬件特征语句。

qq_62634009的博客 712

OpenCV-Python实战(3)——OpenCV中绘制图形与文本

OpenCV 是一个的跨平台计算机视觉库,实现了图像处理和计算机视觉方面的很多通用算法。本文介绍了 OpenCV 提供的与绘制图形和文本相关的函数。在图形方面,我们了解了如何绘制基本图形(直线、矩形和圆形等)以及高级图形(剪裁线、箭头、椭圆和多边形)。在文本方面,我们了解了如何绘制文本以及 OpenCV 库中的所有可用字体。

盼小辉丶的博客 2万+

用vue-cli构建了项目之后在webstorm开发,用npm run dev跑本地服务,经常修改之后在浏览器刷新没反应,偶尔才会有刷新,需要重新跑一遍npm run dev才会更新,这是怎么回事呢?

webstorm保存的时候会先保存到旧时文件中,其实并没有真正保存,要在setting->stystem settings下的“use save write”去掉

java_goodstudy的博客 1万+

国产SerDes芯片(4)—纳芯微 NLS9116+NLS9246

本系列文章主要讲解国产SerDes芯片—纳芯微 NLS9116+NLS9246的相关知识。

零零刷的博客 837

解决在angular 中swiper 不能滑动问题

Swiper是目前较为流行的移动端触摸滑动插件,因为其简单好用易上手,受到很多前端开发者的欢迎。 > 首先提供一种方式 : 监听 ng-repeat 渲染事件 当组件渲染完成时候 初始化 swiper 即可 下面是从网上看到的 今天在使用Swiper的时候遇到这个问题: 使用angularjs动态循环生成swiper-slide类,在swiper-wrapper里生成6个以上的滑

java_goodstudy的博客 6643

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

用html5,nodeJs开发桌面应用程序

原文摘自: http://www.webhek.com/html5-native-desktop-app/ HTML5让Web开发人员用纯HTML技术开发富客户端互联网应用或者甚至本地桌面应用成为了可能。HTML5可以将任何普通网站转变成Web应用。HTML5 web 应用不仅具有在桌面浏览器应用中的优势,同时在智能手机和平板中也是开发利器。 在手机端,有一个非常棒的工具叫做PhoneGap,

java_goodstudy的博客 6285

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

html5调用本地摄像头

THE CURRENT STATE OF HTML5 FORMS Wufoo The accept Attribute accept="image/*"> (images only) Live Demo Firefox 3.6+ Safari 7+ Safari Mobile 5+ Chrome 17

java_goodstudy的博客 6003

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

纯css实现 页面加载动画

http://www.cnblogs.com/lhb25/p/loading-spinners-animated-with-css3.html

java_goodstudy的博客 4388

webpack gulp 实现完整前端工程化

作者:陈学家 链接:https://zhuanlan.zhihu.com/p/21312474 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 在前面的两个小节中已经完整的讲了 webpack 和 gulp 相关的内容,本小节中将会结合二者构建一个完整的前端工作流,内容目录为: 前端工程结构和目标 前端工程目录结构 gulp cl

java_goodstudy的博客 4024

前端上传图片oss

https://segmentfault.com/a/1190000006718963

java_goodstudy的博客 3405

nodejs module.export require 原理分析

大部分实现在module.js中,按照顺序来看: 首先调用require('xx')的时候内部调用了Module._load(path, parent) : Module.prototype.require = function(path) { assert(path, 'missing path'); assert(util.isString(path), 'path must be a

java_goodstudy的博客 3360

AngularJS 报 ngModel:numfmt

AngularJS 报 ngModel:numfmt angularJS 前台报错  Error: ngModel:numfmt Model is not of type `number` 错误:ngModel:numfmt模型的类型是`number` 就是说你input中 model的参数可能是text  然后你在input的typ

java_goodstudy的博客 3042

前端amd,cmd,commonJs,规范

首先说一下前端常用的打包工具 webpack, requierJs,grunt  其中,webpack 为了方便同时支持 amd 与 cmd 规范, NodeJs遵循cmd 规范, 而requireJs 遵循amd 规范, 而浏览器本身是不支持 cmd(同步加载js,加载过程中会造成渲染阻塞,假死现象), 当然amd 是可以得到支持的, 而在webpack 进行js,合并 压缩 隔离之后,commo

java_goodstudy的博客 2839

Vue Lazy Loading Routes

When building apps with a bundler, the JavaScript bundle can become quite large, and thus affect the page load time. It would be more efficient if we can split each route’s components into a separate c

java_goodstudy的博客 2653

js混淆压缩加密分析

请看原文

java_goodstudy的博客 2178

vue-loader + Webpack 环境搭建

https://segmentfault.com/a/1190000004060034

java_goodstudy的博客 1794

html 5 模仿 form 提交file 文件之FormData

使用xhtr 2 可以实现前端提交 无刷新提交form表单 api 参考mdn : https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/%E5%88%A0%E9%99%A4使用ajax 请求时 注意 $.ajax({ url : this.baseUrl+'updateHead',

java_goodstudy的博客 1747
上一篇: Vue Lazy Loading Routes
下一篇: yarn
一朵红杏
博客等级 码龄12年 51粉丝 282原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址