Bluebird-Core API(二)
.error
.error([function(any error) rejectedHandler]) -> Promise
和catch一样,但是catch捕获了所有错误类型的异常,而error捕获是操作异常
注:“errors”意为错误,作为对象能 够instanceof Error,不是字符串、数字等。See a string is not an error.
下面例子跟.catch模式恒等的
// Assumes OperationalError has been made global
function isOperationalError(e) {
if (e == null) return false;
return (e instanceof OperationalError) || (e.isOperational === true);
} // Now this bit:
.catch(isOperationalError, function(e) {
// ...
}) // Is equivalent to: .error(function(e) {
// ...
});
For example, if a promisified function errbacks the node-style callback with an error, that could be caught with
. However if the node-style callback throws an error, only .error
.catch
would catch that.
In the following example you might want to handle just the SyntaxError
from JSON.parse and Filesystem errors from fs
but let programmer errors bubble as unhandled rejections:
var fs = Promise.promisifyAll(require("fs")); fs.readFileAsync("myfile.json").then(JSON.parse).then(function (json) {
console.log("Successful json")
}).catch(SyntaxError, function (e) {
console.error("file contains invalid json");
}).error(function (e) {
console.error("unable to read file, because: ", e.message);
});
Now, because there is no catch-all handler, if you typed console.lag
(causes an error you don't expect), you will see:
Possibly unhandled TypeError: Object #<Console> has no method 'lag'
at application.js:8:13
From previous event:
at Object.<anonymous> (application.js:7:4)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
at startup (node.js:121:16)
at node.js:761:3
( If you don't get the above - you need to enable long stack traces )
And if the file contains invalid JSON:
file contains invalid json
And if the fs
module causes an error like file not found:
unable to read file, because: ENOENT, open 'not_there.txt'
.finally
.finally(function() handler) -> Promise
.lastly(function() handler) -> Promise
传入一个句柄不管Promise结果如果都会执行,返回一个新的Promise,从.finally语义来说,这个句柄(handler)中最终值是不能修改的。
Note: using
for resource management has better alternatives, see resource management.finally
Consider the example:
function anyway() {
$("#ajax-loader-animation").hide();
} function ajaxGetAsync(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest;
xhr.addEventListener("error", reject);
xhr.addEventListener("load", resolve);
xhr.open("GET", url);
xhr.send(null);
}).then(anyway, anyway);
}
This example doesn't work as intended because the then
handler actually swallows the exception and returns undefined
for any further chainers.
The situation can be fixed with .finally
:
function ajaxGetAsync(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest;
xhr.addEventListener("error", reject);
xhr.addEventListener("load", resolve);
xhr.open("GET", url);
xhr.send(null);
}).finally(function() {
$("#ajax-loader-animation").hide();
});
}
Now the animation is hidden but, unless it throws an exception, the function has no effect on the fulfilled or rejected value of the returned promise. This is similar to how the synchronous finally
keyword behaves.
If the handler function passed to .finally
returns a promise, the promise returned by .finally
will not be settled until the promise returned by the handler is settled. If the handler fulfills its promise, the returned promise will be fulfilled or rejected with the original value. If the handler rejects its promise, the returned promise will be rejected with the handler's value. This is similar to throwing an exception in a synchronous finally
block, causing the original value or exception to be forgotten. This delay can be useful if the actions performed by the handler are done asynchronously. For example:
function ajaxGetAsync(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest;
xhr.addEventListener("error", reject);
xhr.addEventListener("load", resolve);
xhr.open("GET", url);
xhr.send(null);
}).finally(function() {
return Promise.fromCallback(function(callback) {
$("#ajax-loader-animation").fadeOut(1000, callback);
});
});
}
If the fade out completes successfully, the returned promise will be fulfilled or rejected with the value from xhr
. If .fadeOut
throws an exception or passes an error to the callback, the returned promise will be rejected with the error from .fadeOut
.
For compatibility with earlier ECMAScript version, an alias .lastly
is provided for
..finally
.bind
.bind(any|Promise<any> thisArg) -> BoundPromise
Same as calling Promise.bind(thisArg, thisPromise)
.
Bluebird-Core API(二)的更多相关文章
- AspNet Core Api Restful 实现微服务之旅 (一)
(一)了解微服务(二)搭建VS项目框架 (三)创建AspNet Core Api VS2017 安装包 链接:https://pan.baidu.com/s/1hsjGuJq 密码:ug59 创 ...
- 【从零开始搭建自己的.NET Core Api框架】(七)授权认证进阶篇
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 【从零开始搭建自己的.NET Core Api框架】(四)实战!带你半个小时实现接口的JWT授权验证
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
C#中缓存的使用 缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可: <%@ Outp ...
- Kubernetes 系列(四):使用Traefik访问.net core api
一. 准备 本篇的要求是在前三篇的基础上已经搭建好的本地k8s以及部署了Traefik,我们将会使用Traefik Ingress来访问.net core api,比较简单,做个记录,如果还没有搭建k ...
- .NET Core API后台架构搭建
ASP.NET Core API后台架构搭建 项目文件:https://files.cnblogs.com/files/ZM191018/WebAPI.zip 本篇可以了解到: 依赖注入 Dapper ...
- Web应用调用.Net Core API
Web应用调用.Net Core API 一.新建Web Application应用: 选择Web Application 新建好之后页面如下: 二.新建Model.新建Model文件夹并建立apiM ...
- 部署.Net Core APi+Vue 到 linux centos 服务器(一)
部署.Net Core APi+Vue 到 linux centos 服务器(一) 前言:项目采用的是 .net core 作为接口,vue作为前端. 此时需要把整个项目架设到linux centos ...
- 《学习笔记》.NET Core API搭建
1.创建 ASP.NET Core Web程序,记住取消HTTPS配置 2.此时一个简单的.NET Core API 架子搭建好了,细心的人可以发现Properties下面不是CS文件,确是launc ...
随机推荐
- jdbc框架 commons-dbutils+google guice+servlet 实现一个例子
最近闲着无聊,于是看了一下jdbc框架 commons-dbutils与注入google guice. 我就简单的封装了一下代码,效率还是可以的.... jdbc+google guice+servl ...
- bat 脚本传递参数测试
start CommissionMQMonitorService.exe MMM 命令 执行器 参数
- Linux屏幕录制gif的工具及教程
准备 要两个工具配合使用.它们可以用命令行安装,也可以用软件管理器安装. 1,byzanz 安装后有两个命令 byzanz-record 录制,既能录制 Gif 动画,又可录制 Ogv 视 ...
- 第三方登录(2)Android客户瑞上第三方登录百度教程
1,在 http://developer.baidu.com/ 注册成开发者 不注册看不到开发相关的链接地址.点自己的用户名,在弹出菜单显示有 <用户中心> ,没有就是没注册. 2,找到 ...
- Server-Side UI Automation Provider - WPF Sample
Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...
- 成功的GIT开发分支模型和策略
详细图文并茂以及git flow工具解释参考: http://danielkummer.github.io/git-flow-cheatsheet/index.zh_CN.html 原文地址:http ...
- Qt之QHeaderView自定义排序(获取正确的QModelIndex)
简述 前几节中分享过关于自定义排序的功能,貌似我们之前的内容已经可以很好地解决排序问题了,但是,会由此引发一些很难发现的问题...比如:获取QModelIndex索引错误. 下面,我们先来实现一个整行 ...
- POJ 1976 A Mini Locomotive【DP】
题意:给出一列火车,可以由三个火车头拉,每个火车头最多拉m节车厢(这m节车厢需要保持连续),再给出n节车厢,每节车厢的人数,问最多能够载多少人到终点. 可以转化为三个长度相等的区间去覆盖n个数,使得这 ...
- Vim实现批量注释的方法
调试代码的时候,免不了要批量注释/取消代码注释,很多IDE都有快捷键将你选中的代码块批量注释/取消注释的功能,那么在Vim里面如何完成这个功能呢? 方法一 块选择模式 批量注释: Ctrl + v 进 ...
- HDU 5328 Problem Killer(水题)
题意: 给一个序列,要找一个等差或等比的连续子序列,求其最长的长度. 思路: 扫两遍,判断等差或等比即可.从左往右扫,维护一个滑动窗口,考虑新加进来的数,如果满足了要求,则更新长度,否则只留最后两个数 ...