-1

Here is the function:

function addUser(request) {
return new Promise((res, rej) => {

    ValidateFormData(request, CREATE_USER, (err) => {if(err) return rej(makeError(err)) });
    console.log('creating user');
    res();
})

}

How can I stop the function when ValidateFormData callback contains err so that console.log('creating user'); won't execute?

Kadiem Alqazzaz
  • 554
  • 1
  • 7
  • 22

4 Answers4

2

You'll want to put that stuff in the else branch inside that callback:

function addUser(request) {
    return new Promise((res, rej) => {
        ValidateFormData(request, CREATE_USER, (err) => {
            if (err) {
                rej(makeError(err));
            } else {
                console.log('creating user');
                res();
            }
        });
    });
}

Putting it outside of the asynchronous callback (regardless whether outside of ValidateFormData, or even outside new Promise) will cause it to run immediately, and you won't have a chance to prevent it retroactively when the error has occurred in the future.

Alternatively, use a then callback to process the result, if there's more than a simple log() that cannot fail:

function addUser(request) {
    return new Promise((res, rej) => {
        ValidateFormData(request, CREATE_USER, (err) => {
            if (err) rej(makeError(err));
            else res();
        });
    }).then(() => {
        console.log('creating user');
        …
    });
}

This is especially useuful as it allows to just util.promisify your ValidateFormData function.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

You need to put everything inside the callback where you can control the code flow of execution and use an if/else for the err:

function addUser(request) {
    return new Promise((res, rej) => {

        ValidateFormData(request, CREATE_USER, (err) => {
            if(err) {
                rej(makeError(err));
            } else {
                console.log('creating user');
                res();
            }
        });
    })
}

Keep in mind that you can also use util.promisify() to convert async callback functions into promise returning functions.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0
ValidateFormData(request, CREATE_USER, (err) => {if(err) return rej(makeError(err)) });
console.log('creating user');

assuming that the ValidateFormData function is async (most likely is, since you have a callback), the following console.log does not wait on it to finish. Hence, it will run whether or not ValidateFormData completes or not.

SerShubham
  • 873
  • 4
  • 10
0

Javascript executes asynchronously, that is why console.log("creating user") gets executed before the callback function in ValidateFormData. Put your console.log in the callback function and everything will be fine.

function addUser(request) {
  return new Promise((res, rej) => {
    ValidateFormData(request, CREATE_USER, (err) => {
      if(err) { 
        return rej(makeError(err)) 
      }
    console.log('creating user'); 
   });
}
Amir Saleem
  • 2,912
  • 3
  • 21
  • 35