gpt4 book ai didi

javascript - API调用和回调

转载 作者:行者123 更新时间:2023-12-01 01:33:09 26 4
gpt4 key购买 nike

我正在尝试创建一条进行 2 个 API 调用的路由,第二个调用取决于第一个调用的结果,并且仅在满足某些条件时才需要调用。以下是我遇到的问题。

  1. 无论如何,finalCheck 函数似乎都会在 forEach 之前调用?即使包含在 if 语句中
  2. 计数器和 userNamesAvailable 在范围之外不可用他们被分配到。
  3. 我确信有更好的方法可以做到这一点,并且希望有任何相关的方法建议。

    router.post('/namecheck', async function(req, res, next) {   
    var name = req.body.name;
    var counter = 0
    var domains = []
    var userNamesAvailable = []
    var dotcom = true
    var answer = ''

    getDomains(checkDomains)

    // Check domain availabilty
    function getDomains (callback) {
    var url='https://api.ote-godaddy.com/v1/domains/available?checkType=FAST'
    Unirest.post(url)
    .headers({
    "Authorization" : "sso-key ",
    "Content-Type": "application/json"
    })
    .send(
    [
    name + '.com',
    name + '.net',
    name + '.org'
    ]
    )
    .end(function(response) {
    domains=response.body.domains
    console.log(domains)
    callback(domains)
    })

    }
    function checkDomains(domains) {
    var d = 0

    //If no data is returned send error
    if(!domains)
    {
    console.log("No data returned")
    return next("No data returned")
    } else {
    //Check how many domains are unavailable
    for(var i=0; i < domains.length; i++) {
    if(domains[i].available == false){
    d = d + 1
    //Flag if the .com is available
    if(domains[i].domain == name + '.com') {
    dotcom = false
    }
    }
    }

    if (d >2) {
    console.log("d is greater than 1")
    answer = 'no'
    //If 2 domains are available and one is .com continue
    } else if (d>1 && dotcom =='false') {
    answer = 'no'
    }
    }
    getUsernames(finalCheck)
    }

    function getUsernames(callback){
    // Social Sites to Check
    var sitenames = [
    'facebook',
    'twitter',
    'instagram',
    'youtube',
    'slack',
    'blogger'
    ]

    // Check Usename availabitliy
    let i = 0
    sitenames.forEach(sitename => {

    Unirest.post('https://api.namechk.com/services/check.json')
    .headers({
    'AUTHORIZATION': 'Bearer ',
    'Accept': 'application/vnd.api.v1+json',
    }).send({
    'site': sitename, username: name,
    }).end(function(response) {
    if(response.body.available == false){
    counter ++
    } else {
    userNamesAvailable.push(response.body)
    }
    });
    i++
    });
    console.log(counter)
    console.log(userNamesAvailable)
    if(i == sitenames.length){
    callback(counter, dotcom)
    }
    }

    function finalCheck(counter, dotcom) {
    console.log('Start finalCheck')
    console.log(counter)
    console.log(dotcom)
    //Final Check for is a name is a go or not
    //If one social site is not available and the
    if(counter == 1 && dotcom =='true') {
    console.log(5-counter + ' social sites available')
    res.send("yes");
    } else {
    res.send("no")
    }
    }
    })

最佳答案

遵循this question上的答案。您应该能够使用 Promise 来做到这一点。

您只需更改函数 getUsernames

  function getUsernames(callback) {
// Social Sites to Check
var sitenames = [
'facebook',
'twitter',
'instagram',
'youtube',
'slack',
'blogger'
];

// Check Usename availabitliy
let requests = sitenames.map((sitename) => {
return new Promise((resolve) => {
Unirest.post('https://api.namechk.com/services/check.json')
.headers({
'AUTHORIZATION': 'Bearer ',
'Accept': 'application/vnd.api.v1+json',
}).send({
'site': sitename,
username: name,
}).end(function(response) {
if (response.body.available == false){
counter++;
} else {
userNamesAvailable.push(response.body);
}
resolve();
});
});
});

Promise.all(requests).then(() => {
console.log(userNamesAvailable);
callback(counter, dotcom);
});
}

function finalCheck() {
console.log('Start finalCheck')
console.log(counter)
console.log(dotcom)
//Final Check for is a name is a go or not
//If one social site is not available and the
if (counter == 1 & dotcom == 'true') {
console.log(5 - counter + ' social sites available')
res.send("yes");
} else {
res.send("no")
}
}

请记住,javascript 中的异步函数是一项功能,而不是您应该反对的东西(我认为您正在尝试将主函数声明为异步函数) 。首先尝试了解它们,您会发现它们是一个很大的优势。

有很多关于它们的文档...... async Fucntion understanding Promises

编辑:不要将您称为全局的变量作为参数发送,它们已经在被调用的函数中可用。

关于javascript - API调用和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com