Paros Kwan
Paros Kwan

Reputation: 63

Nodejs run task in sequence

I am new to node.js and I just don't know how to execute a settimeout function before another function,

for example,

var async = require('async');
function hello(){
    setTimeout(function(){
        console.log('hello');
    },2000);}

function world(){
    console.log("world");
}
async.series([hello,world()]);

and the output is always world hello.

Am I using the library right? I dont the question seems trivial but I really have no idea how to force a short task to run after a long one

Upvotes: 4

Views: 4805

Answers (4)

kailash yogeshwar
kailash yogeshwar

Reputation: 834

You can use callback which are the heart of the nodejs.

var fun1 = function(cb){
 // long task
 // on done 
 return cb(null, result);
}

var fun2 = function(cb){
 return cb(null, data);
}

fun1(function(err, result){
 if(!err){
   fun2(function(er, data){
    // do here last task

   })
 }
}

// to avoid pyramid of doom use native promises

func1 (){
 return new Promise((resolve, reject) => {
   // do stuff here
   resolve(data)
 })

}
func2(){
  return new Promise((resolve, reject) => {
    resolve(data);
  })
}

And then call them using:

func1.then((data) => {
 return func2();
})
.then((resule) => {
 //do here stuff when both promises are resolved
})

Upvotes: -1

Gaurav joshi
Gaurav joshi

Reputation: 1799

Use promise

function hello(){
    return new Promise(function(resolve, reject) {
        console.log('hello');
        resolve();
    });
}

function world(){
   return new Promise(function(resolve, reject) {
      console.log("world");
      resolve();
    });
}


  hello()
  .then(function(){
     return world()
  })
  .then(function(){
    console.log('both done');
  })
  .catch(function(err){
     console.log(err);
  });

Upvotes: 2

Benjamin
Benjamin

Reputation: 3826

You can use promises instead of callbacks. so your code will be something like below:

  var async = require('async');
   function hello(){
   setTimeout(function(){
      console.log('hello');
     },2000);}

   function world(){
    console.log("world");
      }
    return Q.fcall(function() {
       hello();
    })
   .then(function(resultOfHelloFunction){
      world();
 });

The World() function will only be executed when hello() function is completed its execution.

P.S : I am using Q library to promisify functions. It's totally fine to use other libraries (such as bluebird) to achieve the same thing.

Upvotes: 0

Divyanshu Maithani
Divyanshu Maithani

Reputation: 14986

Async requires you to use callback. Follow this link to see some examples. The following code should output hello world correctly:

var async = require("async");
function hello(callback) {
    setTimeout(function(){
        console.log('hello');
        callback();
    }, 2000);
}

function world(callback) {
    console.log("world");
    callback();
}

async.series([hello, world], function (err, results) {
    // results is an array of the value returned from each function
    // Handling errors here
    if (err)    {
        console.log(err);
    }
});

Note that callback() was called inside the setTimeout() function so that it waits for the console.log('hello').

Upvotes: 6

Related Questions