10 examples of 'js wait until function is done' in JavaScript

Every line of 'js wait until function is done' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
84export function waitFor(timeout, done, onCallback, onTimeout) {
85 if (typeof onCallback !== 'function') {
86 onCallback = onCallback
87 ? function (r) { void expect(r).to.be.exist; }
88 : function (r) { expect.fail('unexpected callback: ' + r); };
89 }
90 if (typeof onTimeout !== 'function') {
91 onTimeout = onTimeout
92 ? function () {}
93 : function () { expect.fail('timeout'); };
94 }
95 let to = setTimeout(() => {
96 try {
97 to = null;
98 onTimeout();
99 } catch (e) {
100 return done(e);
101 }
102 return done();
103 }, timeout);
104 return function () {
105 if (to == null) return;
106 try {
107 clearTimeout(to);
108 to = null;
109 onCallback.apply(this, arguments);
110 } catch (e) {
111 return done(e);
112 }
113 return done();
114 };
115}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

139async wait(pageCompleteCheck) {
140 const waitTime = this.options.pageCompleteWaitTime || 5000;
141 if (!pageCompleteCheck) {
142 pageCompleteCheck = this.options.pageCompleteCheckInactivity
143 ? pageCompleteCheckByInactivity
144 : defaultPageCompleteCheck;
145 // if using SPA just override
146 if (this.options.spa) {
147 pageCompleteCheck = spaCheck;
148 }
149 }
150
151 const driver = this.driver,
152 pageCompleteCheckTimeout = this.options.timeouts.pageCompleteCheck;
153 try {
154 const pageCompleteCheckCondition = new Condition(
155 'for page complete check script to return true',
156 function(d) {
157 return d.executeScript(pageCompleteCheck, waitTime).then(function(t) {
158 return t === true;
159 });
160 }
161 );
162 log.debug(
163 `Waiting for script pageCompleteCheck at most ${pageCompleteCheckTimeout} ms`
164 );
165 log.verbose(`Waiting for script ${pageCompleteCheck}`);
166 await timeout(
167 driver.wait(
168 pageCompleteCheckCondition,
169 pageCompleteCheckTimeout,
170 undefined,
171 this.options.pageCompleteCheckPollTimeout || 200
172 ),
173 pageCompleteCheckTimeout,
174 `Running page complete check ${pageCompleteCheck} took too long `
175 );
176 log.debug(`Waiting after load event for ${waitTime} ms.`);
177 await delay(waitTime);
178 } catch (e) {
179 log.error('Failed to wait ' + e);
180 throw new UrlLoadError('Failed to wait ', {
181 cause: e
182 });
183 }
184}
79async function waitFor(testIfDone, timeOutMillis) {
80 let maxtimeOutMillis = timeOutMillis,
81 isDone = false,
82 result = -1;
83
84 async function intervalHandler() {
85 debugLog("intervalHandler");
86 var now = new Date().getTime();
87
88 if (!isDone && ((now - startTime) < maxtimeOutMillis)) {
89 isDone = await testIfDone();
90 return -1; // Not done, try again
91 } else {
92 if (!isDone) {
93 return 3; // Timeout
94 } else {
95 return 0; // Done succesfully
96 }
97 }
98 }
99
100
101 while (result < 0) {
102 debugLog("@@@ wait...: " + result);
103 await wait(100);
104 result = await intervalHandler();
105
106 if (result >= 0) {
107 debugLog("Positive result, fin! " + result);
108 return result;
109 }
110 }
111}
34function waitFor(testIfDone, timeOutMillis) {
35 var maxtimeOutMillis = timeOutMillis,
36 isDone = false,
37 interval;
38
39 function intervalHandler() {
40 var now = new Date().getTime();
41
42 if (!isDone && (now - startTime < maxtimeOutMillis)) {
43 isDone = testIfDone();
44 } else {
45 if (!isDone) {
46 phantom.exit(3); // Timeout
47 } else {
48 clearInterval(interval);
49 phantom.exit(0);
50 }
51 }
52 }
53
54 interval = setInterval(intervalHandler, 100);
55}
49function waitsFor(callback) {
50 if (skipRunsCounter === 0) {
51 jasmineApi.waitsFor.apply(this, arguments);
52 }
53}
24function waitsFor(waitCondition, callback, timeoutMessage, timeout) {
25 if (waitCondition()) {
26 return callback();
27 } else if (timeout > 0){
28 setTimeout(function() {
29 waitsFor(waitCondition, callback, timeoutMessage, (timeout-expect.TIMEOUT_INCREMENT));
30 }, expect.TIMEOUT_INCREMENT);
31 } else {
32 return expect().fail(timeoutMessage);
33 }
34};
41function waitForElement(selector, callback) {
42 if (document.querySelector(selector)) {
43 return callback()
44 } else {
45 setTimeout(waitForElement.bind(this, selector, callback), 300)
46 }
47}
66(function wait() { setTimeout(wait, 5000) })();
203waitForFunction (pageFunction, options = {}, ...args) {
204 const {
205 polling = 'raf',
206 timeout = this._timeoutSettings.timeout()
207 } = options
208 return new WaitTask(
209 this,
210 pageFunction,
211 'function',
212 polling,
213 timeout,
214 ...args
215 ).promise
216}
106export async function wait(timeout: number): Promise<void> {
107 return new Promise<void>((resolve, reject) => setTimeout(resolve, timeout))
108}

Related snippets