如何控制NodeJS中的应用程序stream

我在nodeJS中编写我的第一个应用程序。 我正在写一个电报机器人,我想知道如何控制应用程序的stream程,因为它是asynchronous的。 我来自一个简单的,程序性的,一个接一个的php背景。

比方说,在我的机器人,收到任何消息时,首先程序必须确保用户的详细信息在caching或数据库中,然后再继续。 检查完成后,可以继续。

我打算通过使用一个标志variables来做到这一点,但由于JavaScript的asynchronous性质,无法完成。 我不知道如何去做这件事。 我是否将侦听器分配给对象并发出事件来控制stream?

这是我的代码

const fs = require('fs'); // Establish connection with cache and database const mysql = require('mysql-wrapper'); const Memcached = require('memcached'); const memcached = new Memcached('localhost:11211'); const bb = require('bot-brother'); var settings = { host: 'localhost', database: 'test', user: 'root', }; var qb = require('node-querybuilder').QueryBuilder(settings, 'mysql', 'single'); //Load the database cache functions const dbc = require("./dbc"); dbc.memcached = memcached; dbc.mysqc = qb; //Load the user handling functions const user = require("./user"); user.dbc = dbc; const bot = bb({ key : '331263599:AAHmLl4Zcg4sKGnz7GNzacgkJl1W8lwz33c', polling: { interval: 0, timeout: 1 } }); //code that checks user existence in cache/db bot.api.on('message', (msg)=>{ console.log(msg.from); var userData = msg.from; var tid = userData.id; //Check if user is in cache user.check_user_existence(tid,function(re){ if(re < 2){ user.complete_user_existence(re,userData,function(err,response){ if(err){ bot.api.sendMessage(tid,"Sorry an unexpected error occured!"); } else { console.log('ha'); play = 1; } }); } }); }); //Code to be run after checking if(play==1){ send_another_message(); do_some_things(); } 

您可以使用callback或承诺您可以使用asynchronous模块或互斥体

如果你需要序列化一些asynchronous函数,你可以使用这种方法之一:

回电话

原生承诺

蓝鸟承诺

asynchronous模块

Callback和Promise主要用于第二个function需要第一个function的相关function.bluebird是一个用于创buildpromise并在其上定制的模块。

asynchronous模块是运行asynchronous函数并获得结果的好方法。

最后一种方法是互斥锁,如果您在单个对象或文件中进行asynchronous写入,则需要使用locking释放方法

您可以通过nsynjs同步运行代码。 你的代码可能会变成这样:

第1步:用callback将慢速函数包装到nsynjs-aware包装中:

 // content of wrappers.js user = require("./user"); exports.user_check_user_existence_wrapper = function (ctx, uid) { var res = {}; user.check_user_existence(tid,function(re){ res.re = re; ctx.resume(); }) return res; }; exports.user_check_user_existence_wrapper.nsynjsHasCallback = true; exports.user_complete_user_existence_wrapper = function(ctx, re, userData) { var res = {}; user.complete_user_existence(tid,function(error,ue_response){ res.error = error; res.ue_response = ue_response; ctx.resume(error); // if error is set, it will cause exception in the caller }) return res; }; exports.user_complete_user_existence_wrapper.nsynjsHasCallback = true; 

第2步。把你的同步逻辑的function,使用上面的包装执行慢速function:

 var synchronousCode = function(wrappers,msg) { console.log(msg.from); var userData = msg.from; var tid = userData.id; //Check if user is in cache var re = wrappers.user_check_user_existence_wrapper(nsynjsCtx,tid).re; if(re < 2) try { var res = wrappers.user_complete_user_existence_wrapper(re,userData).ue_response; console.log('ha'); play = 1; } catch (e) { bot.api.sendMessage(tid,"Sorry an unexpected error occured!",e); }; } 

第3步。通过nsynjs运行你的同步函数:

 var nsynjs = require('nsynjs'); var wrappers = require('./wrappers'); var synchronousCode = function(wrappers,msg) { .... } bot.api.on('message', function(msg) { nsynjs.run(synchronousCode,{},wrappers,msg,function(){ console.log('synchronousCode finished'); }) }); 

请在这里看到更多的例子: https : //github.com/amaksr/nsynjs/tree/master/examples/node-module-loading