8000 Use async/await instead of promise chaining (#266) · FZCJ/pyscript@a993d61 · GitHub
[go: up one dir, main page]

Skip to content

Commit a993d61

Browse files
authored
Use async/await instead of promise chaining (pyscript#266)
* Use async/await instead of promise chaining * Await asynchronous operations
1 parent d645722 commit a993d61

File tree

4 files changed

+26
-34
lines changed

4 files changed

+26
-34
lines changed

pyscriptjs/src/App.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
// now we can actually execute the page scripts if we are in play mode
2929
if ($mode == 'play') {
3030
for (let script of $scriptsQueue) {
31-
script.evaluate();
31+
await script.evaluate();
3232
}
3333
scriptsQueue.set([]);
3434
}
3535
3636
// now we call all post initializers AFTER we actually executed all page scripts
37-
setTimeout(() => {
37+
setTimeout(async () => {
3838
for (let initializer of $postInitializers) {
39-
initializer();
39+
await initializer();
4040
}
4141
}, 3000);
4242
};

pyscriptjs/src/components/base.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,23 @@ function createWidget(name: string, code: string, klass: string) {
192192
// ideally we can just wait for it to load and then run. To do
193193
// so we need to replace using the promise and actually using
194194
// the interpreter after it loads completely
195-
// setTimeout(() => {
196-
// this.eval(this.code).then(() => {
197-
// this.proxy = this.proxyClass(this);
198-
// console.log('proxy', this.proxy);
199-
// this.proxy.connect();
200-
// this.registerWidget();
201-
// });
195+
// setTimeout(async () => {
196+
// await this.eval(this.code);
197+
// this.proxy = this.proxyClass(this);
198+
// console.log('proxy', this.proxy);
199+
// this.proxy.connect();
200+
// this.registerWidget();
202201
// }, 2000);
203202
pyodideLoaded.subscribe(value => {
204203
console.log('RUNTIME READY', value);
205204
if ('runPythonAsync' in value) {
206205
runtime = value;
207-
setTimeout(() => {
208-
this.eval(this.code).then(() => {
209-
this.proxy = this.proxyClass(this);
210-
console.log('proxy', this.proxy);
211-
this.proxy.connect();
212-
this.registerWidget();
213-
});
206+
setTimeout(async () => {
207+
await this.eval(this.code);
208+
this.proxy = this.proxyClass(this);
209 8000 +
console.log('proxy', this.proxy);
210+
this.proxy.connect();
211+
this.registerWidget();
214212
}, 1000);
215213
}
216214
});
@@ -272,7 +270,7 @@ export class PyWidget extends HTMLElement {
272270
}
273271
}
274272

275-
connectedCallback() {
273+
async connectedCallback() {
276274
if (this.id === undefined) {
277275
throw new ReferenceError(
278276
`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 {
284282
mainDiv.id = this.id + '-main';
285283
this.appendChild(mainDiv);
286284
console.log('reading source');
287-
this.getSourceFromFile(this.source).then((code: string) => {
288-
this.code = code;
289-
createWidget(this.name, code, this.klass);
290-
});
285+
this.code = await this.getSourceFromFile(this.source);
286+
createWidget(this.name, this.code, this.klass);
291287
}
292288

293289
initOutErr(): void {

pyscriptjs/src/components/pybutton.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ export class PyButton extends BaseEvalElement {
6161

6262
// now that we appended and the element is attached, lets connect with the event handlers
6363
// defined for this widget
64-
setTimeout(() => {
65-
this.eval(this.code).then(() => {
66-
this.eval(registrationCode).then(() => {
67-
console.log('registered handlers');
68-
});
69-
});
64+
setTimeout(async () => {
65+
await this.eval(this.code);
66+
await this.eval(registrationCode);
67+
console.log('registered handlers');
7068
}, 4000);
7169

7270
console.log('py-button connected');

pyscriptjs/src/components/pyinputbox.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ export class PyInputBox extends BaseEvalElement {
4141

4242
// TODO: For now we delay execution to allow pyodide to load but in the future this
4343
// should really wait for it to load..
44-
setTimeout(() => {
45-
this.eval(this.code).then(() => {
46-
this.eval(registrationCode).then(() => {
47-
console.log('registered handlers');
48-
});
49-
});
44+
setTimeout(async () => {
45+
await this.eval(this.code);
46+
await this.eval(registrationCode);
47+
console.log('registered handlers');
5048
}, 4000);
5149
}
5250
}

0 commit comments

Comments
 (0)
0