3

For a project I need to alter some values in a file in five if-statements that are executed one after another, and after all those things are done, I need to save the file. One of the alteration contains an asynchronous save image function. The problem I'm having is that that function has not finished by the time the program gets to the file writing. How should I handle the callbacks within the if-statement? The save image function should be executed before the program proceeds to the next if statement.

if(criterium1){... //Alter file}

if(criterium2){
    saveImage(searchValue, folder,callback(error)){
        ... //Alter file
    }
if(criterium3){... //Alter file}

fs.writeFile(opfFile, xml, function(error) {
    callback(null);
 });

 var saveImage = function(searchValue, folder, callback){
    var images = require ('google-images2');
    images.search(searchValue, function(error, image){
        callback(null);
    });
 };

Can anyone give me any pointer on how to handle this? Thanks! Kind regards, Eva

3 Answers 3

4

as Transcendence said, async is really good for that kind of flow.

I just wrote a simple async-if-else module, because I had a similar problem on my chain steps.

async.waterfall([
   async.if(criterium1, fnCriterium1),
   async.if(criterium2, fnCriterium2),
   async.if(criterium3, fnCriterium3).else(fnCriteriumAlternative)
 ], handler);

I hope this could help you !

cheers

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

Comments

1

You can use the async library, specifically its series control flow. https://caolan.github.io/async/docs.html#series

You want to execute your if statements as tasks in the array (first argument). Make sure to call the callback when your asynchronous actions are done. The second argument is a callback that will execute when all your tasks are done.

async.series([
    function(callback){
        if(criterium1) {
          //Alter file
          // make sure to call the callback
        } else {
          callback(null, null);
        }
    },
    function(callback){
        if(criterium2) {
          //Alter file
          // make sure to call the callback
          // looks like you can just pass callback into the saveImage function
        } else {
          callback(null, null);
        }
    }
], function () {
  fs.writeFile(opfFile, xml, function(error) {
    callback(null);
 });
});

6 Comments

But it's possible that the second statement won't be executed, so then the third statement and the writeFile will not be executed, right?
I am having trouble understanding your example code due to extraneous braces.
I'm sorry. Is this better?
lemme alter my answer
sorry that took a while
|
0

Try wrapping all functions inside a promise

Comments

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.