Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅在解析其他异步/等待函数后调用函数_Javascript_Reactjs_Typescript - Fatal编程技术网

Javascript 仅在解析其他异步/等待函数后调用函数

Javascript 仅在解析其他异步/等待函数后调用函数,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我在单击事件后调用了2个函数 这些函数调用出现在singInHandler()中 需求:我想调用第三个函数window.customAuth.getId(),但只有在上述两个函数分别响应后才能调用它 window.customAuth.init(configFile)@返回带有用户数据的{Promise}承诺 window.customAuth.login()@返回{Promise} 我如何才能做到这一点?将您的承诺放入数组,然后调用Promise.race,它将在解决1个承诺后立即得到解决 或

我在单击事件后调用了2个函数

这些函数调用出现在
singInHandler()中

需求:我想调用第三个函数
window.customAuth.getId()
,但只有在上述两个函数分别响应后才能调用它

window.customAuth.init(configFile)
@返回带有用户数据的{Promise}承诺

window.customAuth.login()
@返回{Promise}


我如何才能做到这一点?

将您的承诺放入数组,然后调用
Promise.race
,它将在解决1个承诺后立即得到解决

或更短:

Promise.race([window.customAuth.init(configFile), window.customAuth.login()]).then(() => {
    // logic here
})

你可以重复你已经做过的事情

signInHandler=async()=>{
wait window.customAuth.init(配置文件);
等待window.customAuth.login();
wait window.customAuth.getId();
}
如果要检查前两个函数的返回值,则。。。就这么做吧

signInHandler=async()=>{
const result1=await window.customAuth.init(配置文件);
const result2=wait window.customAuth.login();
如果(result1==true | |检查您想要的内容){
wait window.customAuth.getId();
}
}

如果您在上述两次调用之后调用它,会发生什么?您所说的“各自的响应”是什么意思?成功地没有任何失败/错误?等待函数解析,这样您就可以将第三个调用放在这两个函数之后。@emilebergron第三个函数使用上述两个函数的成功响应。如果不先执行它们,第三个将不会产生正确的响应。@WebTud是否需要参数?你的意思是说其他两个功能的成功响应?OP的条件不是这些承诺中的“一个”应该得到解决,至少不是我这样理解。同样,赛车在这里也没有真正的意义,因为两个承诺是同时发出的,这与OP在原始代码中编写的不同。
Promise.race([window.customAuth.init(configFile), window.customAuth.login()]).then(() => {
    // logic here
})