Anda di halaman 1dari 8

Asynchronous Javascript

Evozon TechTalk #3
Synchronous code

• Statements executed one after another

• Problem! Execution might be blocked..


Asynchronous code

• Statements schedules something to happen in the future


• Asynchronous != Concurrent/Multi-threaded
How to achieve asynchronicity?

• Treat functions as first class functions/objects:


• Store in variable
• Pass to/Return from another function
Callbacks

• Function passed to another function as a parameter


• E.g. setTimeout, on (jQuery)

• Problem!
• Callback hell
• No ‘throw’ keyword
• Can’t return values
Promises

• Object producing a single value some time in the future


• Use callbacks
• Can be chained
Generators – “Run..Stop..Run”

• Follows Iterator interface


• Pause a function execution -> do other things -> return to function
• yield – pass control to external system
Async/Await

• Still uses promises


• Uses try/catch to handle both sync/async errors

Anda mungkin juga menyukai