js async framework: Q 介绍 ( introducing to Q asynchronous framework)
访问量: 2585
refer to: https://github.com/kriskowal/q
and : https://github.com/kriskowal/q/wiki/API-Reference
使用Q最大的特点是:可以使用 同步的形式来写异步调用的代码:
promise('read a remote url').then('parse text').then('post an artiles').then('get the result').then('add a comment');
Q的命名来历: refer to: http://stackoverflow.com/questions/28006716/what-does-q-q-framework-mean-the-asynchronous-javascript-framework/28006749#28006749
我之前一直没弄明白,后来在stackoverflow 上问了下,才知道是 queue的谐音。
Q 实现了 Promise/A+, 它具有后者约定的所有标准,而且还认为: 如果某个promise的状态是 fullfill, 或者 rejected , 那么就认为它是 resovled.
Q.deffer 有两个方法: resolve, reject , notify, makeNodeResolver
然后,还有 deffer.promise
promise.then( fullfilled, rejected, progress ).. 三个参数。
promise.catch/fail: 用来接收错误。
promise.finally/fi: 用来回收资源,比如 释放数据库的链接啥的。 还会返回个promise
promise.done(fullfilled, rejected, progress) : 用来终止 then的调用。三个参数。
promise.get('something'): 等同于:
promise.then( function( obj){ return obj['something']; });
Q() 方法: 跟jQuery类似,返回一个Q 对象。
Q.nfcall : node function call
Q.fcall: function call