Pencil Code Reference > await

 

await wait for an event

await prevents everything after it from running until a value is returned by a function.

Syntax

await function() defer(x)

await read defer() is an easy way to wait for an input from the user.

Simple Turtle Control Menu

Input the following commands to control the turtle in the example below.

fd: move forward 50
bk: move backward 50
rt: turn right 15
lt: turn left 15

p = write ''
while x isnt 'done'
  await read 'Input: ', defer(x)
  if x is 'fd'
    fd 50
  if x is 'bk'
    bk 50
  if x is 'lt'
    lt 15
  if x is 'rt'
    rt 15
  ct()

await done defer()

await done defer() is the most commonly used form of await, rivaled only by await read and await readnum.

await done defer() is used to prevent everything after it from running until everything before it is done. However, await done defer() is not just a simple pause.