async/await与promise(nodejs中的异步操作问题)
作者:晴天_雨天 时间:2024-04-22 22:43:39
举例写文章详情页面的时候的一个场景:首先更改文章详情中的 PV,然后读取文章详情,然后根据文章详情中文章 Id 查阅该文章评论和该文章作者信息。获取全部数据之后渲染文章详情页。数据库操作都是异步的,最直接想到的办法就是一层一层的回调函数,问题出来了:十分不雅观,要是层再多一点还会有更多麻烦。怎么解决?业内为了处理异步操作问题也是拼了,什么async,q,bluebird,co,处理方式不同,各有千秋,感兴趣可以了解一下,但是惊喜的发现nodejs 7.6已经默认支持ES7中的 async/await 了,结合ES6中的 promise对象,用起来不亦乐乎的。
Async/await的主要益处是可以避免回调地狱(callback hell)问题。
基本概念:
async 表示这是一个async函数,await只能用在这个函数里面。
await 表示在这里等待promise返回结果了,再继续执行。
await 后面跟着的应该是一个promise对象(当然,其他返回值也没关系,不过那样就没有意义了…)
举例:
获取返回值:
var sleep = function (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
// 返回 ‘ok'
resolve('ok');
}, time);
})
};
var start = async function () {
let result = await sleep(3000);
console.log(result); // 收到 ‘ok'
};
捕捉错误:
var sleep = function (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
// 模拟出错了,返回 ‘error'
reject('error');
}, time);
})
};
var start = async function () {
try {
console.log('start');
await sleep(3000); // 这里得到了一个返回错误
// 所以以下代码不会被执行了
console.log('end');
} catch (err) {
console.log(err); // 这里捕捉到错误 `error`
}
};
在循环中:
var start = async function () {
for (var i = 1; i <= 10; i++) {
console.log(`当前是第${i}次等待..`);
await sleep(1000);
}
};
再循环中使用不需要闭包,每次循环会被阻塞。
最前面提到的场景:(综合使用)
var showArticle = async function () {
await new Promise(function (resolve, reject) {
PostModel.incPv(postId, function (result) {
resolve(result);
});
});// pv 加 1
var post = await new Promise(function (resolve, reject) {
PostModel.getPostById(postId, function (article) {
resolve(article);
});
});// 获取文章信息
await new Promise(function (resolve, reject) {
userModel.getUserById(post.author,function (author) {
post.author=author;
resolve();
})
});//获取文章作者
var comments = await new Promise(function (resolve, reject) {
CommentModel.getComments(post._id, function (comment) {
resolve(comment);
});
});// 获取该文章所有留言
for(var i=0;i<comments.length;i++){
await new Promise(function (resolve, reject) {
userModel.getUserById(comments[i].author,function (author) {
comments[i].author=author;
resolve();
})
});//获取文章留言作者
}
if (!post) {
req.session.error = '该文章不存在';
return res.redirect('/post');
}
res.render('post',{post: post, comments: comments});
};
showArticle();
以上所述是小编给大家介绍的async/await与promise(nodejs中的异步操作问题)网站的支持!
来源:http://www.cnblogs.com/lihuanqing/archive/2017/03/03/6495020.html
标签:promise,async,await,nodejs,操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python视频转化字节问题的完整实现
2023-05-21 16:55:28
![](https://img.aspxhome.com/file/2023/2/134942_0s.jpg)
深入了解Django View(视图系统)
2022-07-18 14:47:13
![](https://img.aspxhome.com/file/2023/3/82903_0s.png)
Python排序算法之插入排序及其优化方案详解
2021-04-03 05:39:31
![](https://img.aspxhome.com/file/2023/6/72086_0s.png)
关于Pycharm安装第三方库超时 Read time-out的问题
2022-03-08 10:12:43
![](https://img.aspxhome.com/file/2023/9/118629_0s.png)
python对站点数据做EOF且做插值绘制填色图
2023-03-05 03:30:56
![](https://img.aspxhome.com/file/2023/8/93488_0s.png)
Bootstrap实现响应式导航栏效果
2023-08-13 15:32:13
![](https://img.aspxhome.com/file/2023/8/56048_0s.png)
laravel实现中文和英语互相切换的例子
2024-04-29 14:06:55
![](https://img.aspxhome.com/file/2023/3/130873_0s.jpg)
python中xrange用法分析
2021-10-25 11:51:49
mysql中如何查询数据库中的表名
2024-01-20 03:07:32
golang切片扩容规则实现
2024-04-27 15:31:22
![](https://img.aspxhome.com/file/2023/9/134059_0s.jpg)
使用Python处理json字符串中的非法双引号问题
2021-01-19 19:26:13
PHP Ajax JavaScript Json获取天气信息实现代码
2023-11-20 20:51:27
![](https://img.aspxhome.com/file/2023/1/89731_0s.jpg)
使用Python实现给企业微信发送消息功能
2021-06-10 15:30:07
![](https://img.aspxhome.com/file/2023/0/115630_0s.png)
深入MYSQL字符数字转换的详解
2024-01-18 04:20:11
解决Python中导入自己写的类,被划红线,但不影响执行的问题
2021-07-11 19:10:29
![](https://img.aspxhome.com/file/2023/4/72164_0s.jpg)
python3实现往mysql中插入datetime类型的数据
2024-01-20 12:45:58
php隐藏IP地址后两位显示为星号的方法
2023-08-16 13:05:17
Python基础常用内建函数图文示例解析
2022-05-04 04:54:24
![](https://img.aspxhome.com/file/2023/9/93539_0s.png)
MySQL通配符与正则表达式搜过滤数据详解
2024-01-23 03:12:54
![](https://img.aspxhome.com/file/2023/9/109809_0s.png)
基于Python实现MUI侧滑菜单a标签跳转
2021-06-12 22:49:22
![](https://img.aspxhome.com/file/2023/1/70851_0s.jpg)