Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
return setTimeout(async() => { await sendUser('/register-step1',user.name,user.email,user.role,user.password) await authenticateForUser('/register-authenticate',user.email,user.password) },3000)
await
async function sendUser(url,name,email,role,password) { this.url = url this.name = name this.email = email this.role = role this.password = password this.options = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ firstname: this.name, email: this.email, role: this.role, password: this.password }) } const response = await fetch(this.url,this.options) const data = await response.json() if(data) document.location.replace(`${data.step}`) } async function authenticateForUser(url,email,password) { this.url = url this.email = email this.password = password this.options = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: this.email, password: this.password }) } const response = await fetch(this.url,this.options) const data = await response.json() // if(data) document.location.replace(`${data.step}`) }
async/await
function sendUser(url,name,email,role,password) { this.url = url this.name = name this.email = email this.role = role this.password = password this.options = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ firstname: this.name, email: this.email, role: this.role, password: this.password }) } return fetch(this.url,this.options); // fetch возвращает промис } function authenticateForUser(url,email,password) { this.url = url this.email = email this.password = password this.options = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: this.email, password: this.password }) } return fetch(this.url,this.options); // fetch возвращает промис } setTimeout(async() => { let sendUserResponse = await sendUser('/register-step1',user.name,user.email,user.role,user.password); let sendUserData = sendUserResponse.json(); if (sendUserData) document.location.replace(`${sendUserData.step}`) let authenticateForUserResponse = await authenticateForUser('/register-authenticate',user.email,user.password); let authenticateForUserData = authenticateForUserResponse.json(); // if (authenticateForUserData) document.location.replace(`${authenticateForUserData.step}`) },3000)