How to think when working with promises

This is my first time working with asynchronous programming, especially with promises in Javascript, and I'm really confused about this. I still think we are doing this in C, PHP, C #, etc., where for each step the next one can be sure of completion. This is easy to work because if we have such code (e.g. in C #)

IEnumerable<Page> pages = pagesRepository.getAll();

we can be sure of the next line that all pages are available for use in the object pages. This makes life easier, because we can already work correctly with this data.

Now in JS with promises we will have something like

var pages = dataService.getPages();

and this will not return data, but simply a promise that is very different. Of course, we can add a callback using then, but I'm confused because somewhere else in my code I might need to reference this variable, and I cannot be sure when the callback will be called.

So, what do we think when we deal with these things? Is there some kind of tutorial or several videos that show in detail how to work with asynchronous programming using promises in JS?

+4
source share
2 answers

, , , - , , .

.

" - " .then() , pages. . , - , promises, all().

, , ?

functional!

. , , .

- , , promises JS?

Q - .

+3

, promises #.

, "", "then" "ContinueWith" #.

, , , , promises go

0

Source: https://habr.com/ru/post/1529798/


All Articles