Monday, 5 November 2018

How do I wait for a function (axios) to complete before running a second function?

I would like to have the first function complete as it will return a value to a variable (tokenTenant), that I require to use in the second function.My code:// 1st Function declaration var AxiosRequests = req => { // 1. Retrieve Global Token axios({ method: "post", url: "https://ift.tt/2AMlXMz", data: { username: req.user.api_username, credentials: { password: req.user.api_password } } }).then(res => { var tokenGlobal = res.data.token; // 2. Retrieve Tenant Token axios({ method: "patch", url: "https://ift.tt/2AMlXMz", headers: { "X-Auth-Token": tokenGlobal }, data: { tenant_id: req.user.api_tenant_id } }).then(res => { return (tokenTenant = res.data.token); }); }); }; exports.delOnt = async (req, res) => { await AxiosRequests(req); // <-------- Call 1st Function res.json({ objectVariableRetrievedFromAxiosRequests: tokenTenant }); <---- Call 2nd Function }; Any help is appreciated.

Submitted November 05, 2018 at 01:14PM by asdutoit

No comments:

Post a Comment