Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
27 lines (23 loc) · 1.31 KB

File metadata and controls

27 lines (23 loc) · 1.31 KB
Copy raw file
Download raw file
Edit and raw actions
sidebar_position 1
description Learn the differences between DeviceScript and JavaScript in handling async/await and promises, and how fibers are used in DeviceScript.
keywords
DeviceScript
async/await
promises
fibers
JavaScript

Async/await and promises

The main difference in semantics between DeviceScript and JavaScript is that DeviceScript programs run in multiple fibers (threads). In practice, this behaves like JS with async/await but without an ability to manipulate promises directly (that is fibers can only interleave at await points and at most one fiber runs at any given time).

The compiler enforces usage of await where needed. The Promise<T> is still used just as in TypeScript, but it has no properties.

Technically, the interpreter implements fibers internally (they can be accessed through ds.Fiber class), the await keyword is no-op and the Promise type has no runtime representation. You can inspect the currently running fibers in the debugger and stack traces are not cut at await points (both of which match programmers general (if not JS-specific) expectations).

You can use Function.start(...args: any[]) to start an async or sync function in a new fiber. It will begin executing only after the current fiber hits an await (.start() itself is not async).

Morty Proxy This is a proxified and sanitized view of the page, visit original site.