Answer the question
In order to leave comments, you need to log in
Async in dart?
Hello. Please explain to me a newbie one but. Dart is kind of a single threaded language - it can only do one task at a time? How does the future api work? Runs a type isolate and processes ?
Answer the question
In order to leave comments, you need to log in
For example, you sent a request to the network to get a photo, if a synchronous code was used, then the thread would be blocked and all tasks and functions would be suspended while the photo was coming from the network.
In the asynchronous case, the scheduler does not wait until the response arrives and passes control to the next function in the place where await is written . When a response with a photo arrives, the scheduler will again transfer control to the place where await was written .
The "isolate" in this case is a function wrapped in a Future or with async specified .
- The code inside the isolated function will be executed sequentially.
- Functions wrapped in Future or defined as async will be executed asynchronously, without waiting for the execution of other such asynchronous functions.
- If you call sleep (300) in a normal function, everything will fall asleep for 5 minutes, nothing will happen.
- If you call await sleep (300) in asynchronous, then only this function will fall asleep.
- await tells the scheduler that the function wants to wait here and it can transfer control to any other.
If our world were single-threaded and synchronous , then you wouldn't wake up in the morning if I were still writing this post. If our world weresingle-threaded and asynchronous , then the scheduler would disable me for a while every time await was mentioned in this answer, so that other people could do their own thing.
https://metanit.com/dart/tutorial/7.1.php
https://habr.com/ru/post/442282/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question