Use async/await instead of promise chaining (#266)

* Use async/await instead of promise chaining

* Await asynchronous operations
This commit is contained in:
woxtu
2022-05-10 02:32:01 +09:00
committed by GitHub
parent d645722421
commit a993d61885
4 changed files with 26 additions and 34 deletions

View File

@@ -192,25 +192,23 @@ function createWidget(name: string, code: string, klass: string) {
// ideally we can just wait for it to load and then run. To do
// so we need to replace using the promise and actually using
// the interpreter after it loads completely
// setTimeout(() => {
// this.eval(this.code).then(() => {
// this.proxy = this.proxyClass(this);
// console.log('proxy', this.proxy);
// this.proxy.connect();
// this.registerWidget();
// });
// setTimeout(async () => {
// await this.eval(this.code);
// this.proxy = this.proxyClass(this);
// console.log('proxy', this.proxy);
// this.proxy.connect();
// this.registerWidget();
// }, 2000);
pyodideLoaded.subscribe(value => {
console.log('RUNTIME READY', value);
if ('runPythonAsync' in value) {
runtime = value;
setTimeout(() => {
this.eval(this.code).then(() => {
this.proxy = this.proxyClass(this);
console.log('proxy', this.proxy);
this.proxy.connect();
this.registerWidget();
});
setTimeout(async () => {
await this.eval(this.code);
this.proxy = this.proxyClass(this);
console.log('proxy', this.proxy);
this.proxy.connect();
this.registerWidget();
}, 1000);
}
});
@@ -272,7 +270,7 @@ export class PyWidget extends HTMLElement {
}
}
connectedCallback() {
async connectedCallback() {
if (this.id === undefined) {
throw new ReferenceError(
`No id specified for component. Components must have an explicit id. Please use id="" to specify your component id.`,
@@ -284,10 +282,8 @@ export class PyWidget extends HTMLElement {
mainDiv.id = this.id + '-main';
this.appendChild(mainDiv);
console.log('reading source');
this.getSourceFromFile(this.source).then((code: string) => {
this.code = code;
createWidget(this.name, code, this.klass);
});
this.code = await this.getSourceFromFile(this.source);
createWidget(this.name, this.code, this.klass);
}
initOutErr(): void {