UNPKG

@needle-tools/engine

Version:

Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.

55 lines (52 loc) 1.34 kB
import { Context } from "./engine_setup.js"; /** Wait for a number of seconds to pass. * @param seconds * @param context * @returns Generator function * @example * ```typescript * function *myCoroutineFunction() { * yield* WaitForSeconds(1); * } * ``` */ export function* WaitForSeconds(seconds: number, context: Context | null = null) { const time = context ? context.time : Context.Current.time; const start = time.time; while(time.time - start < seconds) { yield; } } /** Wait for a number of frames to pass. * @param frames * @returns Generator function * @example * ```typescript * function *myCoroutineFunction() { * yield* WaitForFrames(10); * } * ``` */ export function* WaitForFrames(frames: number) { for(let i = 0; i < frames; i++) { yield; } } /** Wait for a promise to resolve. * @param promise * @returns Generator function * @example * ```typescript * function *myCoroutineFunction() { * yield* WaitForPromise(fetch("https://jsonplaceholder.typicode.com/todos/1")); * } * ``` */ export function* WaitForPromise(promise: Promise<any>) { let isRunning = true; promise.then(() => isRunning = false); promise.catch(() => isRunning = false); while (isRunning) { yield; } }