0

I have an isolated scene. And there is a promise in a separate file. But my scene does not want to wait for an answer. And continue to work. And continues to run, how do I make it wait for an answer, and continued to work
file: a

async function apiExchangerate(country, amount) {    
    return new Promise(function (resolve, reject) {
        axios.get(``, {
            headers: { "Accept-Encoding": "gzip,deflate,compress" },
            }).then(
            (response) => {
                var result = String(response.data.result).replace(/\..*/, '');
                console.log('Processing Request');
                resolve(result);
            },
                (error) => {
                reject(error);
            }
        );
    });
}

module.exports = apiExchangerate

file: b

let request_currency = await apiExchangerate(country,amount) // you have to wait for a response, and then continue the work

I want my function to wait for a response and continue executing the script. On the Internet, I have not found an answer to my question.


P.s it doesn't work - What is the explicit promise construction antipattern and how do I avoid it?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    If you already know that your attempt is another case of the _"explicit promise construction anitpattern"_, then why do you add the link with a _"it doesn't work"_ comment (that doesn't explain anything) instead of fixing the anti-pattern? – Andreas Dec 22 '22 at 11:44
  • Do you see the `console.log('Processing Request');` run? If not, the problem is with whatever endpoint you're making your request to not responding – Nick Parsons Dec 22 '22 at 12:28

1 Answers1

3

You're wrapping Promises in Promises for no real reason. One of the reasons why this is an anti-pattern is because it's very easy to get confused by that and to mis-handle resolving/rejecting those Promises.

(My guess is that somewhere you're returning a Promise which resolves to... a Promise. And you're not double-awaiting it so you never resolve the actual Promise.)

Don't over-design it. Simplify the function. axios.get already returns a Promise, and the function is already async, use those:

async function apiExchangerate(country, amount) {
  let response = await axios.get(``, {  headers: { "Accept-Encoding": "gzip,deflate,compress" } });
  let result = String(response.data.result).replace(/\..*/, '');
  console.log('Processing Request');
  return result;
}

Then what you have is a simple async function which will internally await its own operations. And you can await that function:

let request_currency = await apiExchangerate(country, amount);
David
  • 208,112
  • 36
  • 198
  • 279
  • I read that with promis, you can wait for an answer, and then continue the script. But my script still does not wait for an answer and continues to work :( – user20839242 Dec 22 '22 at 11:52
  • @user20839242: When you over-engineer it in the way you have, one of the risks is that you have a Promise which returns a Promise which returns a value. And you only await the “outer” Promise. You’ll need to debug to observe what’s happening, but the problem becomes moot if you avoid such anti-patterns and simplify. Simple code is easier to understand, debug, and support. – David Dec 22 '22 at 11:58
  • I get it. But how do I wait for an answer and get on with my work? (so that my script stops for the response time) – user20839242 Dec 22 '22 at 12:00
  • @user20839242: By using `await` or a callback. But when you start wrapping Promises in Promises then you need to start wrapping awaits in awaits, or callbacks in callbacks. And when you *mix* awaits and callbacks, it gets that much worse. This is exactly why these are anti-patterns to be avoided. – David Dec 22 '22 at 12:12
  • 2
    Good answer, but I don't think it's possible to wrap a promise in a promise, eg: `Promise.resolve(Promise.resolve(1)).then(console.log)` logs 1, not `Promise<1>`, so I don't think the need for double-awaiting here would be needed. – Nick Parsons Dec 22 '22 at 12:17
  • 1
    @NickParsons: Interesting. Admittedly I hadn't explicitly tested the guess part. Though of course the OP shouldn't need to worry about it in the first place by just simplifying. – David Dec 22 '22 at 12:23
  • I mean, there's no way I can wait for an answer and keep working? – user20839242 Dec 22 '22 at 12:26
  • @user20839242: You keep asking this, and it's unclear what you're really talking about. To "wait for an answer" you [await the Promise](https://stackoverflow.com/q/29516390/328193) (either with the `await` keyword or a `.then()` callback). That's it. That's *the entirety* of it. Whatever you've over-complicated beyond that, don't over-complicate it. At this point you're essentially saying, "Okay, I get that I need to use `await` to wait for the Promise... but how do I wait for the Promise?" – David Dec 22 '22 at 12:28