0

I have an async function like the one below. Note that the timeout function is just symbolic of other code that runs in an async way. I want the code to wait till each of the series block is done before returning to the final function.

var async = require('async')
var param1 = 'foobar'

function withParams(param1, callback) {
    console.log('withParams function called')
    console.log(param1)
    callback()
}
function withoutParams(callback) {
    if(true){
        console.log("withoutParams function called")
        setTimeout(function () {
            console.log("test")        }, 10000);
        callback()
    }
    else{
        console.log('This part will never run')
        callback()
    }

}
async.series([
    function(callback) {
        withParams(param1, callback)
    },
    withoutParams
], function(err) {
    console.log('all functions complete')
})

The output is

withParams function called
foobar
withoutParams function called
all functions complete
test

I want the output to be

withParams function called
foobar
withoutParams function called
test
all functions complete

Is there any other async version that waits for the final block to finish before calling the ending function(err){... part ? I am learning nodejs.

1 Answer 1

1

You need to just move your callback:

function withoutParams(callback) {
    if(true){
        console.log("withoutParams function called")
        setTimeout(function () {
            console.log("test")
            callback() //  <-- HERE
        }, 1000);

    }
    else{
        console.log('This part will never run')
        callback()
    }

}

Of course you can get the same results without the async library by just using promises:

var param1 = 'foobar'

function withParams(param1) {
  console.log('withParams function called')
  console.log(param1)
}

function withoutParams() {
  return new Promise((resolve, reject) => {
    if (true) {
      console.log("withoutParams function called")
      setTimeout(function() {
        console.log("test")
        resolve()
      }, 1000);
    } else {
      console.log('This part will never run')
      reject()
    }
  })
}

withParams(param1)
withoutParams()
  .then(() => console.log('all functions complete'))

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, the first option isn't really a good option since the timeout function just signifies a bunch of code that needs to change. Can I however chain promises and async function?
Regardless of which method you end up with, either the callback or the resolve() needs to happen when the async code runs. In that sense the second version isn't much different than the first — just replaces the callback with resolve(). But you can definitely chain then()s just return something from one and it will get picked up as an argument in the next one. If that something is a promise, it will work asynchronously.
Ok thanks, lets say instead of console.log, i wanted to return values. Can i put that in resolve(value) ?
Yes! You can resolve(value) and then it will show up in .then(value => /* use value here */)
Thanks so much , if i want to parse both err and value, would i do something like resolve(err,value) and then for displaying can i do then (err,value) => if (err){console.log("Error")} else{console.log(value)}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.