duanguai2781 2017-05-29 16:23
浏览 87
已采纳

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

I'm writing my first application in nodeJS. I'm writing a telegram bot, and i was wondering how to control the flow of the application given it asynchronous nature. I come from a php background where everything was simple, procedural, one after the other.

Lets say, in my bot, when any message is received, first the program must make sure the user details are in the cache or database before proceeding. After the check is done it can proceed.

I was going to do this by using a variable for a flag, but it cannot be done because of the asynchronous nature of javascript. I have no idea how to go about doing this. Do i assign listeners to an object and emit events to control the flow?

Here is my code

    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();
}
  • 写回答

2条回答 默认 最新

  • douwu5009 2017-05-29 17:28
    关注

    You can use callbacks or promises You can use async module or mutexes

    If you need serialize some asynchronous functions you can use one of this approaches:

    Callback

    Native promises

    Bluebird promise

    Async module

    Callback and Promise mostly used for related functions which second function needs first function .bluebird is a module for creating promises and having full customize on it.

    Async module is good way to run functions asynchronous and get result of them together.

    Last way is Mutex if you had asynchronous write in single object or file you need lock-release approach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的