[英]How to wait for the response from other call to create a request for post call
I have below 2 files, I want to make sure the call is in order.我有以下 2 个文件,我想确保通话正常。 I tried the promise and callback, I have to admit, I am not 100% familiar with async calls.
我尝试了 promise 和回调,我不得不承认,我不是 100% 熟悉异步调用。
config.js:配置.js:
import rolesJson from '../../roles';
class Config{
url;
rolesList;
constructor(callback){
var baseurl = 'www.example.com/env';
fetch(baseurl)
.then(response => response.json())
.then(data => {
this.url = data.url;
getAuth(data.env);
}).catch((error) => {
});
const getAuth= (env) => {
const headers = { 'Content-Type': 'application/json' };
const options = { method: 'POST', headers, body:JSON.stringify(rolesJson(env))};
console.log("THIS BODY SHOULD NOT BE UNDEFINED", options.body);
fetch('www.example.com/auth', options)
.then(response => response.json())
.then(data => {
}).catch((error) => {
});
};
}
}
module.exports = Config;
roles.js角色.js
var roleUrl = 'www.example.com/roles';
const setEnviroment = (rolesdata,env) => {
let reqBody = {
"environment": env,
"components": rolesdata
}
console.log("REQUEST BODY CREATED", reqBody);
return req;
}
const getRoles = (env) => {
fetch(roleUrl)
.then(response => response.json())
.then(roles => {
let rolesList = [];
roles.map(x => {
const roleObj = {
name: x.name,
id: x.id,
}
rolesList.push(roleObj);
})
return setEnviroment(rolesList, env);
}).catch((error) => {
});
};
module.exports = getRoles;
How can I make sure when i am calling fetch('www.example.com/auth', options), the options.body is not undefined?如何确保当我调用 fetch('www.example.com/auth', options) 时,options.body 不是未定义的? I have tried to use async/await and callbacks, nothing works for me.
我曾尝试使用 async/await 和回调,但对我没有任何作用。
Any help will be very appreciated.任何帮助将不胜感激。
Thanks谢谢
No worries - promises are not easy to get at first.不用担心 - 一开始并不容易得到承诺。 So first of all, you can only rely on the value, if you waited that it was resolved.
所以首先,如果你等待它被解决,你只能依赖这个值。 This can be done, as you already pointed out, with.then or with async / await.
正如您已经指出的那样,这可以通过.then 或异步/等待来完成。
.then-solution: .then-解决方案:
var roleUrl = 'www.example.com/roles';
const setEnviroment = (rolesdata,env) => {
let reqBody = {
"environment": env,
"components": rolesdata
}
console.log("REQUEST BODY CREATED", reqBody);
return req;
}
const getRoles = (env) => {
fetch(roleUrl)
.then(response => response.json())
.then(roles => {
let rolesList = [];
roles.map(x => {
const roleObj = {
name: x.name,
id: x.id,
}
rolesList.push(roleObj);
})
return setEnviroment(rolesList, env);
});
// we return the promise
};
module.exports = getRoles;
class Config{
url;
rolesList;
constructor(callback){
var baseurl = 'www.example.com/env';
fetch(baseurl)
.then(response => response.json())
.then(data => {
this.url = data.url;
getAuth(data.env);
}).catch((error) => {
});
const getAuth= (env) => {
const headers = { 'Content-Type': 'application/json' };
const options = { method: 'POST', headers, body:JSON.stringify(rolesJson(env))};
console.log("THIS BODY SHOULD NOT BE UNDEFINED", options.body);
fetch('www.example.com/auth', options)
.then(response => response.json());
// we return the Promise
};
}
}
module.exports = Config;
// calling method
Config.getAuth(env).then((value) => {
return getRoles(env); //this returns a Promise again
}).then(x => {
// here you have the return type of getRoles
})
async-await-solution:异步等待解决方案:
var roleUrl = 'www.example.com/roles';
const setEnviroment = (rolesdata,env) => {
let reqBody = {
"environment": env,
"components": rolesdata
}
console.log("REQUEST BODY CREATED", reqBody);
return req;
}
const getRoles = async (env) => {
let response await fetch(roleUrl); // awaiting fetch promise
let roles = await response.json(); // awaiting .json()-promise
let rolesList = [];
roles.map(x => {
const roleObj = {
name: x.name,
id: x.id,
}
rolesList.push(roleObj);
})
return setEnviroment(rolesList, env);
};
// async always returns a promise
module.exports = getRoles;
class Config{
url;
rolesList;
constructor(callback){
var baseurl = 'www.example.com/env';
fetch(baseurl)
.then(response => response.json())
.then(data => {
this.url = data.url;
getAuth(data.env);
}).catch((error) => {
});
const getAuth = async (env) => {
const headers = { 'Content-Type': 'application/json' };
const options = { method: 'POST', headers, body:JSON.stringify(rolesJson(env))};
console.log("THIS BODY SHOULD NOT BE UNDEFINED", options.body);
const response = await fetch('www.example.com/auth', options);
const body = await response.json();
return body; // we return a Promise including the body
};
}
}
module.exports = Config;
// calling method
const callerMethod = async () => {
const auth = await Config.getAuth(env);
const roles = await getRoles(env);
//now you can work with the resolved stuff
};
Please note, that callerMethod will return a Promise itself again, because it's async.请注意, callerMethod 将再次返回 Promise 本身,因为它是异步的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.