[Javascript] Promise-based functions should not throw exceptions
You can also start a chain of then()
method calls via Promise.resolve()
and execute the synchronous code inside a callback:
function asyncFunc() {
return Promise.resolve()
.then(() => {
doSomethingSync();
return doSomethingAsync();
})
.then(result => {
···
});
}
An alternative is to start the Promise chain via the Promise constructor:
function asyncFunc() {
return new Promise((resolve, reject) => {
doSomethingSync();
resolve(doSomethingAsync());
})
.then(result => {
···
});
}
This approach saves you a tick (the synchronous code is executed right away), but it makes your code less regular.
[Javascript] Promise-based functions should not throw exceptions的更多相关文章
- 【译】JavaScript Promise API
原文地址:JavaScript Promise API 在 JavaScript 中,同步的代码更容易书写和 debug,但是有时候出于性能考虑,我们会写一些异步的代码(代替同步代码).思考这样一个场 ...
- JavaScript Promise:去而复返
原文:http://www.html5rocks.com/en/tutorials/es6/promises/ 作者:Jake Archibald 翻译:Amio 女士们先生们,请准备好迎接 Web ...
- Effective Java 61 Throw exceptions appropriate to the abstraction
Exception translation: higher layers should catch lower-level exceptions and, in their place, throw ...
- [Javascript] Promise
Promise 代表着一个异步操作,这个异步操作现在尚未完成,但在将来某刻会被完成. Promise 有三种状态 pending : 初始的状态,尚未知道结果 fulfilled : 代表操作成功 r ...
- Javascript Promise 学习笔记
1. 定义:Promise是抽象异步处理对象以及对其进行各种操作的组件,它把异步处理对象和异步处理规则采用统一的接口进行规范化. 2. ES6 Promises 标准中定义的API: ...
- javaScript Promise 入门
Promise是JavaScript的异步编程模式,为繁重的异步回调带来了福音. 一直以来,JavaScript处理异步都是以callback的方式,假设需要进行一个异步队列,执行起来如下: anim ...
- JavaScript Promise异步实现章节的下载显示
Links: JavaScript Promise:简介 1.一章一章顺序地下载显示下载显示 使用Array.reduce()和Promise.resolve()将各章的下载及显示作为整体串联起来. ...
- [Typescript] Promise based delay function using async / await
Learn how to write a promise based delay function and then use it in async await to see how much it ...
- Javascript Promise入门
是什么? https://www.promisejs.org/ What is a promise? The core idea behind promises is that a promise r ...
随机推荐
- WebGoat学习——跨站脚本攻击(Cross‐Site Scripting (XSS))
跨站脚本攻击(Cross‐Site Scripting (XSS)) XSS(Cross Site Script)跨站脚本攻击.是指攻击者向被攻击Web 页面里插入恶意html代码,当用户浏览该页之时 ...
- AutoCompleteTextView使用 监听
AutoCompleteTextView使用 An editable text view that shows completion suggestions automatically while t ...
- [POJ] #1004# Financial Management : 浮点数运算
一. 题目 Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 173910 Acc ...
- [cocos2d-js]长按按钮事件
定义两个全局变量 var bLeftButtonClick = false; var bRightButtonClick = false; var MainLayer = cc.Layer.exten ...
- Operation not applicable
ClientDataSet.DataSetProvider1 Operation not applicable ClientDataSet1->Open(); 解决办法 1.update new ...
- (window)Android Studio安装以及Fetching android sdk component information超时的解决方案
转自:http://www.cnblogs.com/sonyi/p/4154797.html 在经过两年的开发之本后,Google 公司终于发布了 Android Studio 1.0,喜欢折腾的童鞋 ...
- centos安装postfixadmin
postfixadmin的安装,跟普通网站安装没什么区别 配置好虚拟目录,然后在数据库中创建数据库postfix 修改config.inc.php文件,详细搜索谷歌 访问http://www.你的域名 ...
- Codeforces 682 D. Alyona and Strings (dp)
题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...
- Ajax.ActionLink与Ajax.BeginForm使用场所的思考
Ajax.ActionLink使用在提交参数明确的情况下,如: Ajax.ActionLink("加入购物车", "AddToCart", "Cart ...
- [OAuth2 & OpenID] 1.OAuth2授权
1 OAuth2解决什么问题的? 举个栗子先.小明在QQ空间积攒了多年的照片,想挑选一些照片来打印出来.然后小明在找到一家提供在线打印并且包邮的网站(我们叫它PP吧(Print Photo缩写