L
L
Leo Mavrin2018-08-09 04:59:53
Asynchronous programming
Leo Mavrin, 2018-08-09 04:59:53

How to know when a function is initialized?

There are two documents that need to be loaded asynchronously. From document A , a function is called which is in document B . The problem is that a race condition occurs. A function is called that does not yet exist. Is there a way to check for the existence of a function and only then call it!? It would be even better if the function could notify some handler that it was created. But, it is necessary that the "handler" was also already created. Since everything is loaded asynchronously, it is not always possible to guess what will appear faster.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2018-08-09
@Leo5878

Promises are found in some programming languages. A "promise" that the page will load. You need a construction like in JavaScript Promise.all([ P1, P2 ]);- after both promises are fulfilled, you can call the function.

// псевдокод
var docA, docB;
Pa = asyncload("url-A"); // ф-я возвращает промис
Pb = asyncload("url-B"); 
Promise.all([ Pa, Pb ]).then( docA.b_is_ready() );
// метод объекта docA  "b_is_ready" вызывает функцию в документе B

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question