JS中Promise 的使用
class A:
public static
testFunc(): (() =>
Promise<boolean>)[]
{
cc.log("进入第一层Promise")
let tempLoadTemplates = [
["First Func", function() { cc.log("this is function 1");} ],
["Second Func",
function() { cc.log("this is function 2");} ],
["Thrid Func", function() { cc.log("this is function 3");} ],
["Fouth Func", function() { cc.log("this is function 4");} ],
];
let testPrmise : (()=>Promise<boolean>)[] = [];
for (var
i = 0;
i != 4; i++)
{
cc.log("I 的值为 : " +
i);
const
TempTable = tempLoadTemplates[i];
const
name = TempTable[0];
const
fuc = TempTable[1];
const
ci = i;
testPrmise.push(()
=> {
return
new Promise<boolean>(async (completed,
failed) => {
cc.log("这是第一层Pormis ====>" +
"Step : " + ci);
//==>输出正常
cc.log("这是第一层Pormis ====>" +
"Step : " + i);
//==>输出 4
//结论 对于Promise中的闭包参数传值 必须用const 修饰符 否则将被赋值为最后一次设定的值
//这边就可以在这里让TestFunction做相应的操作
//比如读取数据 等等
await
this.TestFunction(name,
fuc);
if (i %
2 == 0)
completed(true);
else
failed("Do Failed");
})
});
}
cc.log("压入完毕!");
cc.log(testPrmise);
return
testPrmise;
}
private static
TestFunction(name,
functionAddr)
{
cc.log(name);
functionAddr();
}
class B: