Asynchronous programming practice of when.js under node.js_node.js
Assume a business scenario:
Get the rss through the rss address and save it in the file, and the rss address is saved in the file.
To complete the business of this scenario, you need to complete 3 tasks:
1. Read the rss address from the file.
2. Get rss.
3. Save in file.
Finally integrate these three tasks.
Preparation:
The file that stores the rss address, address.txt.
http://programmer.csdn.net/rss_programmer.html
Task 1:
Read the contents of the rss address file and return it through callback.
var getRssAddress = function(path, callback) {
fs.readFile(path, {encoding: 'utf8'}, function (err, data) {
callback(err, data);
});
}
Mission 2:
Get rss through rss address and return error or data through callback.
var getRss = function(url, callback) {
var data = '';
http.get(url, function(res) {
res.on('data', function(chrunk) {
data = chrunk;
});
res.on('end', function() {
callback(null, data);
});
}).on('error', function(err) {
callback(err, null);
});
}
Mission 3:
Save RSS in file and return error through callback.
var saveRss = function(data, callback) {
fs.writeFile('rss.txt', data, 'utf8', function(err) {
callback(err);
});
}
Integration:
getRssAddress('address.txt', function(err, data) {
if(err) {
console.log(err);
Return;
}
getRss(data, function(err, data) {
If(err) {
console.log(err);
Return;
}
SaveRss(data, function(err) {
If(err) console.log(err);
});
});
});
The above code is fully asynchronous processing, using the most common callback to handle the return of asynchronous logic. The advantage is that it is a standard writing method that everyone can easily accept; the disadvantage is that the coupling is too strong, handling exceptions is troublesome, and the code is not intuitive, especially When dealing with complex business logic and multiple processing tasks, layers of callbacks can make people’s eyes dazzle, making the code difficult to maintain.
when.js, one of the implementations of the Promise/A specification, is aimed at this problem domain.
Let’s take a look at the transformed code.
Mission 1:
var getRssAddress = function(path) {
var deferred = when.defer();
fs.readFile(path, {encoding: 'utf8'}, function (err, data) {
If (err) deferred.reject(err);
deferred.resolve(data);
});
return deferred.promise;
}
Mission 2:
var getRss = function(url) {
var deferred = when.defer();
var data = '';
http.get(url, function(res) {
res.on('data', function(chrunk) {
data = chrunk;
});
res.on('end', function() {
deferred.resolve(data);
});
}).on('error', function(err) {
deferred.reject(err);
});
return deferred.promise;
}
任务3:
var saveRss = function(data) {
var deferred = when.defer();
fs.writeFile('rss.txt', data, 'utf8', function(err) {
if(err) deferred.reject(err);
deferred.resolve();
});
return deferred.promise;
}
整合:
getRssAddress('address.txt')
.then(getRss)
.then(saveRss)
.catch(function(err) {
console.log(err);
});
解释:
promise/A规范定义的“Deferred/Promise”模型就是“发布/订阅者”模型,通过Deferred对象发布事件,可以是完成resolve事件,或者是失败reject事件;通过Promise对象进行对应完成或失败的订阅。
在Promises/A规范中,每个任务都有三种状态:默认(pending)、完成(fulfilled)、失败(rejected)。
1.默认状态可以单向转移到完成状态,这个过程叫resolve,对应的方法是deferred.resolve(promiseOrValue);
2.默认状态还可以单向转移到失败状态,这个过程叫reject,对应的方法是deferred.reject(reason);
3.默认状态时,还可以通过deferred.notify(update)来宣告任务执行信息,如执行进度;
4.状态的转移是一次性的,一旦任务由初始的pending转为其他状态,就会进入到下一个任务的执行过程中。
按照上面的代码。
通过when.defer定义一个deferred对象。
var deferred = when.defer();
异步数据获取成功后,发布一个完成事件。
deferred.resolve(data);
异步数据获取失败后,发布一个失败事件。
deferred.reject(err);
并且返回Promise对象作为订阅使用。
return deferred.promise;
订阅是通过Promise对象的then方法进行完成/失败/通知的订阅。
getRssAddress('address.txt')
.then(getRss)
then有三个参数,分别是onFulfilled、onRejected、onProgress
promise.then(onFulfilled, onRejected, onProgress)
上一个任务被resolve(data),onFulfilled函数就会被触发,data作为它的参数.
上一个任务被reject(reason),那么onRejected就会被触发,收到reason。
任何时候,onFulfilled和onRejected都只有其一可以被触发,并且只触发一次。
对于处理异常,when.js也提供了极其方便的方法,then能传递错误,多个任务串行执行时,我们可以只在最后一个then定义onRejected。也可以在最后一个then的后面调用catch函数捕获任何一个任务的异常。
如此写法简单明了。
getRssAddress('address.txt')
.then(getRss)
.then(saveRss)
.catch(function(err) {
console.log(err);
});
Promise给异步编程带来了巨大的方便,可以让我们专注于单个任务的实现而不会陷入金字塔厄运,以上代码仅仅是基本使用,when.js提供的功能远远不止本文提到的这些,具体参照官方API。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Summary: Asynchronous programming in C++ allows multitasking without waiting for time-consuming operations. Use function pointers to create pointers to functions. The callback function is called when the asynchronous operation completes. Libraries such as boost::asio provide asynchronous programming support. The practical case demonstrates how to use function pointers and boost::asio to implement asynchronous network requests.

As web applications become more complex, programmers are forced to adopt asynchronous programming to handle large numbers of requests and I/O operations. PHP: HypertextPreprocessor is no exception. To meet this need, ReactPHP has become one of the most popular asynchronous programming frameworks for PHP. In this article, we will discuss how to do asynchronous programming in PHP using ReactPHP. 1. Introduction to ReactPHP ReactPHP is an event-driven programming

JavaScript Function Asynchronous Programming: Essential Skills for Handling Complex Tasks Introduction: In modern front-end development, handling complex tasks has become an indispensable part. JavaScript function asynchronous programming skills are the key to solving these complex tasks. This article will introduce the basic concepts and common practical methods of JavaScript function asynchronous programming, and provide specific code examples to help readers better understand and use these techniques. 1. Basic concepts of asynchronous programming In traditional synchronous programming, the code is

How to implement asynchronous message processing in PHP Introduction: In modern web applications, asynchronous message processing is becoming more and more important. Asynchronous message processing can improve the performance and scalability of the system and improve the user experience. As a commonly used server-side programming language, PHP can also implement asynchronous message processing through some technologies. In this article, we will introduce some methods of implementing asynchronous message processing in PHP and provide code examples. Using Message Queuing Message Queuing is a way of decoupling system components, allowing different components to

In-depth understanding of the new features of PHP8: How to use asynchronous programming and code efficiently? PHP8 is the latest major version of the PHP programming language, bringing many exciting new features and improvements. One of the most prominent features is support for asynchronous programming. Asynchronous programming allows us to improve performance and responsiveness when dealing with concurrent tasks. This article will take an in-depth look at PHP8’s asynchronous programming features and introduce how to use them efficiently. First, let’s understand what asynchronous programming is. In the traditional synchronous programming model, code follows a linear sequence

3 common problems and solutions in asynchronous programming in Java frameworks: Callback Hell: Use Promise or CompletableFuture to manage callbacks in a more intuitive style. Resource contention: Use synchronization primitives (such as locks) to protect shared resources, and consider using thread-safe collections (such as ConcurrentHashMap). Unhandled exceptions: Explicitly handle exceptions in tasks and use an exception handling framework (such as CompletableFuture.exceptionally()) to handle exceptions.

The Go framework uses Go's concurrency and asynchronous features to provide a mechanism for efficiently handling concurrent and asynchronous tasks: 1. Concurrency is achieved through Goroutine, allowing multiple tasks to be executed at the same time; 2. Asynchronous programming is implemented through channels, which can be executed without blocking the main thread. Task; 3. Suitable for practical scenarios, such as concurrent processing of HTTP requests, asynchronous acquisition of database data, etc.

1. Why use asynchronous programming? Traditional programming uses blocking I/O, which means that the program waits for an operation to complete before continuing. This may work well for a single task, but may cause the program to slow down when processing a large number of tasks. Asynchronous programming breaks the limitations of traditional blocking I/O and uses non-blocking I/O, which means that the program can distribute tasks to different threads or event loops for execution without waiting for the task to complete. This allows the program to handle multiple tasks simultaneously, improving the program's performance and efficiency. 2. The basis of Python asynchronous programming The basis of Python asynchronous programming is coroutines and event loops. Coroutines are functions that allow a function to switch between suspending and resuming. The event loop is responsible for scheduling
