如何跳过node.js中的asynchronous调用
有什么方法可以跳过串行或并行呼叫stream程中的呼叫。
var flow = require('flow'); flow.exec( function(){ //Execute }, function(){ //Skip }, function(){ //Exec }, function(){ //Done } );
只需在您可能想跳过的方法内创build一个条件,并立即触发callback
flow.exec( function taskOne() { // long task fs.readFile(path, 'utf8', this); }, function taskTwo() { if (condition) { return this(); // trigger the callback. } }, function lastTask() { console.log("done"); } );
function getUser(userId,username,callback){ flow.exec( function(){ if(userId) return this(null,userId); redis.get('username:'+username,this); }, function(err,userId){ if(err) throw err; if(!userId) return callback(null); this.userId = userId; redis.hgetall('user:'+userId+':profile',this); }, function(err,profile){ if(err) throw err; profile.userId = this.userId; callback(profile); } ); } getUser(null,'gk',function(user){ if(!user) console.log('not found'); console.log(user); });
我可以这样使用吗?
- 在Firebase云端函数中使用toLocaleString格式化货币值
- Nodejs错误:无效:inherits@2.0.0 / usr / lib / node_modules / inherits @ 2
- 可以在本地运行Grunt Karma吗?
- Nodejs / npm:如何重新安装/重新编译复制的应用程序包
- Node.js套接字服务器如何知道客户端连接到哪个IP或域?
- Ember.js,Express.js和Node.js的资产pipe道?
- 如何识别下面的代码模式
- 如何使用phantomjs或其他浏览器在nodejs中创buildWeb代理?
- 无法修改和testing分叉的GitHub库 – npm ERR! 版本未find