简体   繁体   中英

ES6 exit function from inside another function callback

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?

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.

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.

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.

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'); 
   });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM