0
  exports.addAccount = async function(username, password){
        var request = require('request');
        var data = '{"login": "' + username + '","password":"' + password + '" }';
        var json_obj = JSON.parse(data);
        error = request.post({
            headers: {'content-type': 'application/json'},
            url: 'https://example.su/engine/auth.php',
            form: json_obj
        }, async function* (error, response, body){
            try {
              response = JSON.parse(body);
              if(response.error){
                  return 'error' + response.error;
              }else{
                try {
                    if(response.id){
                      const ret = ConfigManager.addAuthAccount(response.id, response.hash,  
                      response.email, response.username)
                      if(ConfigManager.getClientToken() == null){
                          ConfigManager.setClientToken(response.hash);
                      }
                      ConfigManager.save();
                      return ret;
                    }
                  } catch (e) {
                      return "error";
                  }
              }
            } catch (e) {
                 return "error";
            }
        });
        if(error.includes('error')){
                //*****************
        }
    }

How to redirect return from a nested function? or how to write the response body to the variable 'error'? How can this be implemented? redo the authorization mechanism in the program?

  • Hello @Денис, welcome to StackOverflow! Could you edit your question, cause I don't think we really understood what you ask and which problem you face? I could guess but it seems that it wouldn't be appropriate. I will try making an edit for you, and if it seems to be what you are asking for, and you accept it I will help you with that. – Irfandy Jip Dec 07 '19 at 15:06
  • I need to get a response from a server outside of request.post – Денис Лебедев Dec 07 '19 at 15:07
  • Yes, that was what I was expecting. Since the answer has been closed, I think I should just give you a reference. I think you should read [this](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). From what I see your problem is related to 'Asynchronous Calls', and I would recommend you to take a second and understood what is asynchronous and callbacks are. – Irfandy Jip Dec 07 '19 at 15:24

1 Answers1

1

First of all you do not need to use async unless you are using await inside the function. Second, to solve this issue you need to return a promise and resolve (return result values) or revoke (return errors) it accordingly.

exports.addAccount = function(username, password){
    return new Promise((resolve, revoke) => {
        var request = require('request');
        var data = '{"login": "' + username + '","password":"' + password + '" }';
        var json_obj = JSON.parse(data);
        error = request.post({
            headers: {'content-type': 'application/json'},
            url: 'https://example.su/engine/auth.php',
            form: json_obj
        }, function* (error, response, body){
            try {
            response = JSON.parse(body);
            if(response.error){
                revoke('error' + response.error);
            }else{
                try {
                    if(response.id){
                    const ret = ConfigManager.addAuthAccount(response.id, response.hash,  
                    response.email, response.username)
                    if(ConfigManager.getClientToken() == null){
                        ConfigManager.setClientToken(response.hash);
                    }
                    ConfigManager.save();
                    resolve(ret);
                    }
                } catch (e) {
                    revoke("error");
                }
            }
            } catch (e) {
                revoke("error");
            }
        });
        if(error.includes('error')){
                //*****************
        }
    });
}

You can find more about promises here.

MEDZ
  • 2,227
  • 2
  • 14
  • 18