526860
代码:
// create.ts
export function create() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log('create');
res('123');
}, 3000);
});
}
// getInstance.ts
let instance = null;
export async function getInstance() {
if (!instance) {
instance = await create();
}
return instance;
}
// test.ts
async function one() {
const o = await getInstance();
console.log(o);
}
async function two() {
const t = await getInstance();
console.log(t);
}
one();
two();
执行结果如下
create
create
123
123
请教一下如何修改getInstance.ts才能让create只执行一遍