The Nodejs of async in the middle of the night

Source: Internet
Author: User

The Nodejs of async in the middle of the night

Finally again back to Nodejs asynchronous, I previously thought that async in my writing, has been written, now only found that there are a lot of places did not think clearly, the following one by one illustrate.

Module synchronization and Connection async

You should always use Express for website development. Express original problem is not the focus, you must use the third tool, Redis, MySQL, and so on.

Redis requires a connection, and a callback is required for the connection to succeed (he is an asynchronous). Here's the question: is the Redis first successful, or is the express first successful?

redis.on('ready', cb);app.listen(300, cb);

Which one succeeds first? If you think my question is not quite clear, we have made Redis's preparation successful for 10 minutes, so you can see that Redis is not ready yet, Express is running and the website is running successfully. If Redis is tight as the cache is OK, if it is some useful data, then there may be users can not access the site, you can change Redis to other components, that is, Nodejs in the asynchronous has affected the component, but you still follow the idea of synchronization to use, to reference.

This requires a description, the boot order of the components, to ensure that the components are connected properly to start the Web site.

Control from the root

Where to deal with it?

I think it's still going to be handled from the root, the async in the Nodejs is not guaranteed in order, and there's no guarantee that the code you put in front of you must run on the front end. What you need is to describe the dependencies and boot order of your modules from the root, so that the usability of the site can be guaranteed.

But is it okay to write like this?

redis.on('ready',=>{    app.listen(300, cb)})

May be yes, but don't you feel uncomfortable?

So, I do, so there are modules in the boot order, all with a function description, to ensure the boot sequence and dependency, so I use the Async.auto module, he can explicitly explain the sequence of functions:

ConstAsync= require(' Async ')function Main(){  Async.Auto({    Config(CB){      CB(NULL, require('./config '))},    Logger:[' config ',(Scope,cb= {      ConstLogger= require('./module/logger ')(Scope.Config)CB(NULL,Logger}],    Redis:[' config ',(Scope,cb= {      ConstRedis= require('./module/redis ')CB(NULL,Redis}],    util(CB){      CB(NULL, require('./module/util '))},    Module:[' util ', ' Logger ', ' Redis ',(Scope,cb= {      CB()}],    Web:[' config ', ' module ',(Scope,cb= {      ConstWeb= require('./web ') (Scope,cb}],     Ready:[' module ', ' web ',(Scope,cb= {      CB()}]}, function(Err,Scope{    ConstLogger= Scope.Logger    Logger.Info(' app running ')})}Main()

This is my function, and the Web module is also organized so that the third-party modules are ready when the Web is started.

Having figured this out, I rewritten my own project again and welcomed the onlookers:

Express-boilerplate

The Nodejs of async in the middle of the night

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.