Bluebird-NodeJs的Promise
Promise是异步代码实现控制流的一种方式。这一方式可以让你的代码干净、可读并且健壮。
比如,你用来异步处理文件事件的回调代码:
fs.readFile('directory/file-to-read', function(err, file){
if (error){
//handle error
} else {
//do something with the file
}
});
你以前可能听说过Node很快会陷入回调地狱,以上就是原因。作为一个node开发者你会遇到很多的异步代码,也就会遇到很多的回调(callback)。
这些回调还是比较简单的。但是你会需要在一个动作完成之后继续做别的动作,因此回调会不断的嵌套。你会发现很快这些代码就很难阅读了,更别提维护了。比如:
fs.readFile('directory/file-to-read', function(err, file){
if (error){
//handle error
} else {
//do something with the file
fs.mkdir('directory/new-directory', function(err, file){
if (error) {
//handle error
} else {
//new directory has been made
fs.writeFile('directory/new-directory/message.txt', function(err, file){
if(error) {
// handle error
} else {
// File successfully created
}
});
}
});
}
});
在上面的例子中我想要异步的读取一个文件,然后创建一个目录并创建一个文件。你可以看到这个简单的三步走的任务变成了多么丑陋的嵌套的代码,尤其是一旦你再在这些代码中添加逻辑控制的时候,代码更是不可想象丑陋。
我们为什么要在node中使用Promise
以上面对的代码作为例子,我们将探究如何用Promise解决上面说到的问题:
fs.readFileAsync('directory/file-to-read')
.then(function(fileData){
return fs.mkdirAsync('directory/new-directory');
})
.then(function(){
return fs.writeFileAsync('directory/new-directory/message.txt');
})
Promise给我们提供了一个更加清晰和健壮的方式编写异步代码。最开始的方法返回一个promise,这个promise上可以调用‘then’方法。‘then’之后还可以调用更多的‘then’。每个‘then’都可以访问上一个‘then’返回的信息。每一个‘then’方法返回的示例都可以再调用一个‘then’。这往往就是另一个异步的调用。
Promise也让你更加容易的把代码分解到多个文件中。比如:
function readFileandMakeDirectory(){
return fs.readFileAsync('directory/file-to-read')
.then(function(fileData){
return fs.mkdirAsync('directory/new-directory');
});
}
//The following will execute once the file has been read and a new directory has been made
readFileandMakeDirectory()
.then(function(){
return fs.writeFileAsync('directory/new-directory/message.txt');
})
很容易创建返回promise的方法。当你需要把代码分解到不同的文件中的时候会非常有用。比如,你可能有一个路由读取一个文件,读取其内容,并且把文章内容以json的形式返回出来。你可以把代码分解成多个返回promise的组件。
//routes/index.js
var router = require('express').Router();
var getFileExcerpt = require('../utils/getFileExcerpt') router.get('/', function(){
getFileExcerpt.then(function(fileExcerpt){
res.json({message: fileExcerpt});
});
}); module.exports = router; //utils/getFileExcerpt.js var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs')); module.exports = function getPost(){
return fs.readFileAsync(file, 'utf8').then(function(content){
return {
excerpt: content.substr(0, 100)
}
});
}
以上代码也清楚的表明任何一个返回的‘then’后面可以再调用‘then’。
处理错误
使用promise处理错误非常简单。当执行一堆‘then’方法的过程中出现错误的时候Bluebird会找到最近的.catch方法执行。你可以在‘then’链中嵌入catch方法。上例可以改写为:
fs.readFileAsync('directory/file-to-read')
.then(function(fileData){
return fs.mkdirAsync('directory/new-directory');
})
.then(function(){
return fs.writeFileAsync('directory/new-directory/message.txt');
})
.catch(function(error){
//do something with the error and handle it
});
你可以使用catch处理错误。
但是我要用的模块不返回promise
你会注意到上面的例子中使用了‘fs.writeFileAsync'和’fs.mkdirAsync’。如果你检查node的文档你会看到这些方法并不存在。FS不会返回promise。
尽管如此,bluebird提供了一个非常有用的功能来promise化不返回promise的模块。比如,promise化fs模块,只需要简单地require bluebird模块和一个被promise化的fs模块。
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
创建你自己的Promise
因为你可以promise化模块,所以你不会需要写很多的代码来创建promise。然后,即使如此知道如何创建也是很必要的。创建promise需要提供resolve和reject的回调方法。每一个都需要传对了:
//myPromise.js
var Promise = require('bluebird');
module.exports = function(){
return new Promise(function(resolve, reject){
tradiationCallbackBasedThing(function(error, data){
if (err) {
reject(err);
} else {
resolve(data)
}
});
});
}
这样就完成了promise化。接下来,你就可以使用这个技术把你想要promise化的都写成promise的形式。
测试Promise
当我测试服务端代码的时候,我最喜欢的框架是mocha和chai。需要注意,测试异步代码的时候你需要告诉mocha异步代码什么时候执行完成。否则,他只会继续执行下面的测试,这时就会出错。
这个时候,只需要简单地调用mocha在it部分中提供的回调方法:
it('should do something with some async code', function(done){
readPost(__dirname + '/../fixtures/test-post.txt')
.then(function(data){
data.should.equal('some content inside the post');
done();
})
.catch(done);
});
Promise非常有用,使用node编写异步代码的时候强烈建议你使用promise。
可以通过阅读Bluebird的API文档了解更多。
Bluebird-NodeJs的Promise的更多相关文章
- Nodejs Q promise设计思路
Nodejs Q promise库 前言 Q库为nodejs提供了一个基于promise的编程方式,从此避免了一层又一层的callback调用.不过Q的灵活性也给我造成了很大困扰,我可以用promis ...
- nodejs与Promise的思想碰撞
玩node的同志们都知道,当这门语言被提出来的时候,作为自己最为骄傲的异步机制,却被PHP和Python等战团喷得不成样子的是,他们嘲笑着nodejs那蠢蠢的无限嵌套,nodejs战团只能以我们只要性 ...
- bluebird -1 New Promise方法
new Promise new Promise(function(function resolve, function reject) resolver) -> Promise 创建一个Prom ...
- nodejs使用promise实现sleep
个人博客 地址:http://www.wenhaofan.com/article/20181120180225 let sleep = function (delay) { return new Pr ...
- Nodejs Promise的一点记录
项目需要,看了点nodejs,其中比较难理解的就是Promise了,记录一下学习bluebird提供的Promise实现. Promise.promisifyAll(obj)方法 作用:把对象的方法属 ...
- NodeJs回调操作Promise化
mongoose是一个NodeJs下MongoDB的ORM库.使用这个库,您从DB到表(collection)都不用创建了.只需要在项目中定义好Model. 下面就是用上一篇的代码来演示如何把mong ...
- promise 进阶 —— async / await 结合 bluebird
一.背景 1.Node.js 异步控制 在之前写的 callback vs async.js vs promise vs async / await 里,我介绍了 ES6 的 promise 和 ES ...
- 深入理解jQuery、Angular、node中的Promise
最初遇到Promise是在jQuery中,在jQuery1.5版本中引入了Deferred Object,这个异步队列模块用于实现异步任务和回调函数的解耦.为ajax模块.队列模块.ready事件提供 ...
- q.js实现nodejs顺序调用
nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise 什么是promise promise一个标准,它描述了异步调用的返回结果,包括正确 ...
- 如何把函数都用promise方式实现?
如何把函数都用promise方式实现? 我觉得这是一个好问题.当前在我所在的公司,只要用 NodeJS 进行开发,从框架到具体的应用实例到工具,已经全部迁移到以 promise 为中心开发方式.带来的 ...
随机推荐
- DateFormat工具类
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java. ...
- redis启动.停止.重启
Linux下安装 ]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz ]# tar xzf redis-2.8.17.tar.g ...
- oracle vm中的xp添加共享文件夹
接着就可以在虚拟的电脑系统里面打开我们的共享文件夹,在桌面找到”网络邻居“,双击打开 我们需要通过”添加一个网络邻居“来加载我们刚才添加的”共享文件夹“,根据向导一步步执行 然后点击”浏览 ...
- How to Pronounce OPPORTUNITY
How to Pronounce OPPORTUNITY Share Tweet Share Take the opportunity to learn this word! Learn how t ...
- Haskell语言学习笔记(66)Aeson
Data.Aeson 安装 aeson $ cabal install aeson Installed aeson-1.2.3.0 Prelude> :m +Data.Aeson Prelude ...
- a,b = b,a 换值问题
a = "hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhe ...
- 常用类一一MATH类一一两个静态常量PI 和E,一些数学函数。
package test; public class MathTest { public static void main(String[] args) { System.out.println(Ma ...
- spring boot 启动方式
一:IDE 运行Application这个类的main方法 二:在springboot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...
- org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]
运行servlet程序报错: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Cat ...
- Win32 Debug & Release
今天帮汤老师调试程序,他生成的程序不能运行,怀疑子程序之间编译顺序的问题:我试了之后,也出现同样的问题,但是把Win32 Debug 换成Win32 Release却可以运行了. 网上搜索了下,在CV ...