วิธีที่จะรอสำหรับการตอบรับจากอีกโทรหาเพื่อสร้างการร้องขอสำหรับตำแหน่งโทรหา

0

คำถาม

ฉันมี 2 ด้านล่างแฟ้ม,ฉันต้องการเพื่อให้แน่ใจว่าที่อยู่ในคำสั่ง ฉันพยายามที่สัญญาและเรียกกลับผมต้องยอมรับว่าผมไม่ได้ชัวร์ 100%หคุ้นเคยกับ async โทรมา

config.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

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;

ผมต้องทำยังไงให้แน่ใจว่าตอนที่ผมโทรเรีย('www.example.com/auth',ตัวเลือก)ที่ตัวเลือกร่างกายของไม่ทำดัชนีเทียบสี? ฉันพยายามที่จะใช้ async/รอคำสั่งและคอลแบกไม่มีอะไรทำงานสำหรับฉัน

ที่เขาแนะนำให้ผูกผ้ากันเปื้อนจะเป็นเกียรติอย่างยิ่ง

ขอบคุณ

1

คำตอบที่ดีที่สุด

0

ไม่เป็นไร-สัญญานี่ไม่ใช่เรื่องง่ายที่จะเป็นอัจฉริยะจริงๆนะ ดังนั้นอย่างแรก,คุณจะสามารถพึ่งพาค่าถ้าคุณรอว่ามันถูกแก้ไขเรียบร้อยแล้ว เธอจะได้ไม่ต้องทำอย่างที่คุณอยู่แล้วเล็งออกไปด้วยงั้นหรือกับ async/รอคำสั่ง.

.งั้น-ทางออก:

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-รอคำสั่ง-ทางออก:

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
};

โปรดจำไว้ว่า callerMethod จะกลับมาเป็นสัญญากับตัวเองอีกครั้งเพราะมันเป็น async.

2021-11-23 07:44:07

มีทางจะไม่สร้างวิธีอื่นอีกเหมือน callerMethod? อย่างคงเปลี่ยนแปลง roles.js หรือ getAuth ใน config.js
Tian Qin

ใช่คุณต้องการพระเจ้าเหมือนกันงั้นแล้ว.ตามนั้นถ้าคุณต้องการ นี่คือเหตุผลหลาย devs ใช้ async องเจอเรื่องนั้น codebase
Marcel Cremer

ฉันพยายามจะเพิ่ม.งั้นแล้ว.จับใน getAuth นี่คือไม่ได้ทำงาน..และเรื่องทั้งหมดมันห่ออยู่ใน constructor ฉันไม่สามารถให้ async บ constructor..
Tian Qin

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่