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.
Mission 2:
Get rss through rss address and return error or data through callback.
Mission 3:
Save RSS in file and return error through callback.
Integration:
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:
return deferred.promise;
}
return deferred.promise;
}
任务3:
return deferred.promise;
}
整合:
解释:
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函数捕获任何一个任务的异常。
如此写法简单明了。
Promise给异步编程带来了巨大的方便,可以让我们专注于单个任务的实现而不会陷入金字塔厄运,以上代码仅仅是基本使用,when.js提供的功能远远不止本文提到的这些,具体参照官方API。