-1

I have this angularJS function

$scope.myFunc = ()  => {
        myModule.getConfig().update(params);
        myModule.go();

        myModule.log('ok');
    };

and the go function

go: function () {
            $state.go('myFunc', options);
        },

myModule.go() is calling an API and taking some time. How can I modify this code to call myModule.log() only when the previous line has finished?

user3174311
  • 1,714
  • 5
  • 28
  • 66
  • 1
    Your question doesn't have enough information about how `go()` works for us to determine what mechanisms it has to signal that it is finished (assuming it has any). – Quentin Aug 03 '22 at 07:23
  • https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Quentin Aug 03 '22 at 07:24
  • @Quentin you are correct, I updated the question. it uses uirouter/angularjs I think. – user3174311 Aug 03 '22 at 08:12

1 Answers1

-2

Assuming myModule.go takes a callback, you could use a Promise.

$scope.myFunc = async () => {
    try {
        myModule.getConfig().update(params);
        await new Promise((resolve, reject) => myModule.go(resolve));
        myModule.log('ok');
    } catch (error) {
        console.error(error);
    }
};
cooldogyum
  • 72
  • 8