04async await
async
//函数的三种定义
async function hello() { return "Hello" };
let hello = async function() { return "Hello" };
let hello = async () => { return "Hello" }; //使用
hello().then((value) => console.log(value))
hello().then(console.log) //简写
async函数返回值决定了返回的promise的状态
async function fn() {
// throw 1 走catch
// return Promise.reject(1) 走catch
return 2 //走then
return Promise.resolve(2) //走then
return undefined //默认返回 走then
}
await
function fn3() {
return Promise.resolve(3)
}
const fn4 = async () => {
console.log(1); //第一步
const res = await fn3() //await右边是promise对象,得到的结果是promise成功的value值
console.log('res: ', res); //第三步
}
let res=fn4()
console.log(2); //第二步
async function fn5(){
const data=await 30 //await右边不是promise,得到的结果是塔本身,即右边的值
console.log('data: ', data);//
}
fn5()
try-catch
function fn3() {
return Promise.reject(3)
}
const fn4 = async () => {
console.log(1); //第一步
try {
const res = await fn3() //await右边是promise对象,得到的结果是promise成功的value值
console.log('res: ', res); //第三步
} catch (error) {
console.log('error: ', error);//3 promise失败的状态值
}
}
fn4()
04async await的更多相关文章
- async & await 的前世今生(Updated)
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...
- [.NET] 利用 async & await 的异步编程
利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html 目录 异步编程的简介 异 ...
- [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程
怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html ...
- [.NET] 利用 async & await 进行异步 IO 操作
利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html 序 上次,博主 ...
- [C#] 走进异步编程的世界 - 开始接触 async/await
走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...
- [译] C# 5.0 中的 Async 和 Await (整理中...)
C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...
- await and async
Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...
- C# await和async
基础阅读:http://www.cnblogs.com/jesse2013/p/async-and-await.html 答疑阅读:http://www.cnblogs.com/heyuquan/ar ...
- C#~异步编程再续~await与async引起的w3wp.exe崩溃-问题友好的解决
返回目录 关于死锁的原因 理解该死锁的原因在于理解await 处理contexts的方式,默认的,当一个未完成的Task 被await的时候,当前的上下文将在该Task完成的时候重新获得并继续执行剩余 ...
随机推荐
- Linux学习笔记之linux软件包安装以及源的替换
先是软件源的替换,在刚安装的Ubuntu中会配有原先的软件源,所以如果要替换时,可在网上找与自己ubuntu相对应的软件源,比如我的ubuntu版本为12.04,所以我得找到相对应能够适用Ubuntu ...
- 从零搭建Spring Boot脚手架(2):增加通用的功能
1. 前言 今天开始搭建我们的kono Spring Boot脚手架,首先会集成Spring MVC并进行定制化以满足日常开发的需要,我们先做一些刚性的需求定制,后续再补充细节.如果你看了本文有什么问 ...
- C# Thread.Name 的作用和意义
Thread.Name属性 C#的线程提供Thread.Name属性.这意味着每个线程可以设定一个Name属性来标志它们. Name属性的使用时特性 线程的Name属性默认情况下是null.该值只能被 ...
- 【Linux】添加Nginx代理配置只允许内部IP访问
location / { index index.jsp; proxy_next_upstream http_500 http_502 http_503 http_504 error timeout ...
- Flutter build apk 如何访问网络
将下列配置放到路径:your_project\android\app\src下的 main 文件夹下的 AndroidManifest.xml 和 profile 文件夹下的 AndroidManif ...
- vue cli3.0使用svg全过程
VUE-cli3使用 svg-sprite-loader svg-sprite-loader 的插件,用来根据导入的 svg 文件自动生成 symbol 标签并插入 html 1.安装依赖 npm i ...
- 浏览器自动化的一些体会7 selenium webdriver的一些问题
1. 下载图片 这个链接说得最好,差不多所有可能的方法都列举了,除了没有提到用URLDownloadToFile,不过这和用WebClient差不多. https://stackoverflow.co ...
- pyttsx3 的使用教程
import pyttsx3 def use_pyttsx3(): # 创建对象 engine = pyttsx3.init() # 获取当前语音速率 rate = engine.getPropert ...
- OptaPlanner的新约束表达方式 Constraint Streams
有好些时间没有写过关于OptaPlanner的东西了,其实近半年来,OptaPlanner还是推出了不少有用.好用的新特性.包括本文讲到的以Stream接口实现评分编程.关于OptraPlanner的 ...
- React 17 要来了,非常特别的一版
写在前面 React 最近发布了v17.0.0-rc.0,距上一个大版本v16.0(发布于 2017/9/27)已经过去近 3 年了 与新特性云集的 React 16及先前的大版本相比,React 1 ...