How to program asynchronously?

  node.js, question

The synchronous programming thinking of node.js asynchronous good head big.
I want to export

a1
a1-end
a2
a2-end
Run OVER

I just can’t get out.
The error code is as follows:


async=require("async"); a1=function(cb){ setTimeout(function(){console.log("a1")}, 2000); cb(function(){console.log("a1-end")}); bracket a2=function(cb){ setTimeout(function(){console.log("a2")}, 2000); cb(function(){console.log("a2-end")}); bracket async.series([ function(cb) { a1(cb); }, function(cb) { a2(cb); bracket ], function(err, results) { console.log("Run OVER") });

After studying, answer it yourself, but I still don’t understand it very well:

javascript1async=require("async");
 
 a1=function(cb){
 setTimeout(function(){console.log("a1");
 console.log("a1-end");
 cb();
 }, 5000);
 bracket
 
 
 
 
 a2=function(cb){
 setTimeout(function(){console.log("a2");
 console.log("a2-end");
 cb();
 }, 2000);
 
 
 bracket
 
 async.series([
 function(cb) {
 a1(cb);
 },
 function(cb) {
 a2(cb);
 bracket
 ],
 
 function(err, results) {
 console.log("Run OVER")
 });