1
resolve(parent, args) {

  let argumentCheck = (args.title && args.content && args.author_id);
  let author_idCheck = false;

  try {
    if(argumentCheck ==  undefined) {
      throw new Error("New article arguments are not defined!");
    }
    else {
      console.log("ArgumentCheck passed!")
    }

    userModel_i.findById(args.author_id, (error, userObject)=>{
      if(userObject !== undefined){
        author_idCheck = true;
        console.log(author_idCheck)
        //If this is placed outside of callback then author_idCheck undefined
        //Because code is asynchornous takes time for callback function run
        //Therefore console.log run before callback finishes hence undefined
      }
    })

    console.log("run")

    let articleModel = new articleModel_i({
      title: args.title,
      content: args.content,
      author_id: args.author_id, //Author ID should come from somewhere in applciation
      createdAt: String(new Date())
    })

    return articleModel.save();

  }
  catch(e) { console.log(e) }

}

I have 2 blocks of code:

  1. The code inside the callback function for findById and
  2. the code after condole.log("run").

The callback function is taking time to run, so the console.log() and code after runs first. The problem is the code after the condole.log("run") depends on the code in the callback of code block 1, so the callback should be run first. I can't place the code after the console inside the callback because the return statement would then be for the callback and not the resolve function.

Is there any way to make the callback run first then the code after console? I'm thinking maybe passing them both inside a function and run them in there?

u-ways
  • 6,136
  • 5
  • 31
  • 47
Aura Divitiae
  • 105
  • 2
  • 9
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –  Sep 07 '18 at 13:12
  • 1
    uses the Promises Api or even better the async/await https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – Panos K Sep 07 '18 at 13:52

1 Answers1

1

findById returns Query if you did not specify a callback. Which got a then-able method that returns a Promise. So you can refactor your code to use .then to find userModel_i then create an articleModel after the asynchronous task:

// return the promise
return userModel_i.findById(args.author_id)
  .then(userObject => {
    if(userObject !== undefined){
      author_idCheck = true;
      console.log(author_idCheck)
    }
    return userObject;
  })
  .then(userObject => {
    // push code block 2 inside the promise resolver 
    console.log("run")

    let articleModel = new articleModel_i({
      title: args.title,
      content: args.content,
      author_id: args.author_id,
      createdAt: String(new Date())
    })

    // return article when resolved
    return articleModel.save();
  });
u-ways
  • 6,136
  • 5
  • 31
  • 47
  • 1
    Don't you have to return something from the first then so that the second then is resolvable? – fllprbt Sep 07 '18 at 13:33