【发布时间】:2019-12-01 02:05:34
【问题描述】:
基本上我有开关盒。在其中,我将不得不执行完全依赖于其他要执行的函数的函数。
Switch(){
case '1':
//Executes first always
function a(){
..
..
..
b().then(){
//Execute this block only after function b is executed
}
..
..
..
c().then(){
//Execute this block only after function c is executed
}
..
..
}
break;
case '2':
function b(){
}
break;
case '3':
function c(){
}
break;
}
在上面的例子中,case '1' 将首先被执行。在里面,我将不得不等待function b() 执行然后继续执行
函数 a()。就像function c() 一样明智。如何做到这一点?
【问题讨论】:
-
Switch(){语法不正确,仅声明一个函数不会调用它 - 要调用一个函数,请将()s 放在其名称后,例如a(); -
@CertainPerformance 将是解释的错字。我在 switch 语句中有函数。
-
b是否返回承诺?如果没有,你就不能使用then(),你只需要把代码按正确的顺序排列,它们就会按照你的意愿执行。 -
这似乎行不通……你想让这些函数做什么?
标签: javascript function promise wait