gpt4 book ai didi

javascript - 在不支持的 api 调用上抛出错误

转载 作者:行者123 更新时间:2023-12-03 07:57:01 24 4
gpt4 key购买 nike

我们使用带有成功/失败回调的第 3 方 API,如下使用:

    ThirdPartyAPI.checkIfLoggedIn(function (response) {
if (response.status === 'connected') {
console.log("success");
} else {
console.log("failed");
}
}
});

他们的 API 没有错误处理程序,我们遇到了这样的情况:我们没有从 API 调用中得到任何返回值——它消失在以太中。我想知道其他人如何处理这种情况——您是否将这些调用包装在 setTimeout 中,然后自己抛出错误?其他选择?

最佳答案

如果您无法自行修改checkIfLoggedIn,那么这是您唯一的解决方案。它看起来像这样(超时时间为 5 秒):

function checkIfLoggedIn ( ) {

var max_wait = 5000,
timed_out = false;

var timer = setTimeout( function ( ) {
timed_out = true;

// Handle the timeout here
console.log( "timeout" );

}, max_wait );

ThirdPartyAPI.checkIfLoggedIn(function (response) {

if ( timed_out === true ) {
// Ignore the response if we already handled a timeout
return;
}

clearTimeout( timer );

if (response.status === 'connected') {
console.log("success");
} else {
console.log("failed");
}

});
}

关于javascript - 在不支持的 api 调用上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755402/

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