10 examples of 'wait for async function to finish javascript' in JavaScript

Every line of 'wait for async function to finish javascript' 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
87function waitForScript(cb) {
88
89 var tasks = [];
90
91 if (delayScript) {
92 tasks.push(function(cb) {
93 invoker(_callUserScript(delayScript), function(err) {
94 if (err) {
95 return cb('wait-for-script (global-level) still not returning a truthy value, timed out after ' +
96 (maxTries * tryTimeout) + ' ms.');
97 }
98 cb();
99 });
100 });
101 }
102
103 if (userPage['wait-for-script']) {
104 tasks.push(function(cb) {
105 invoker(_callUserScript(userPage['wait-for-script']), function(err) {
106 if (err) {
107 return cb('wait-for-script (page-level) still not returning a truthy value, timed out after ' +
108 (maxTries * tryTimeout) + ' ms.');
109 }
110 cb();
111 });
112 });
113 }
114
115 components.forEach(function(component) {
116 if (!component['wait-for-script']) return;
117 tasks.push(function(cb) {
118 invoker(_callUserScript(component['wait-for-script']), function(err) {
119 if (err) {
120 return cb('wait-for-script (component-level) still not returning a truthy value for component ' +
121 '\'' + component.name + '\', timed out after ' + (maxTries * tryTimeout) + ' ms.');
122 }
123 cb();
124 });
125 });
126 });
127
128 async.waterfall(tasks, function(err) { cb(err); });
129}
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

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}
31(function waitForResult() {
32 if (self._results === null) {
33 log('Waiting for trace complete');
34 window.setTimeout(waitForResult, 1000);
35 } else {
36 cb(self._results);
37 }
38}());
619function waitForAsync(counter) {
620 // If all asynchronous elements have been loaded init panels
621 // If we have been waiting for more than 1s give up and do with what we have
622 if (!document.getElementsByClassName('xwiki-async').length || counter > 1000) {
623 panelEditorInit();
624 } else {
625 counter += 10;
626 setTimeout(waitForAsync, 10, counter);
627 }
628}
142function waitFor(timeout) {
143 return new Promise((resolve) => {
144 setTimeout(() => {
145 resolve(true);
146 }, timeout);
147 });
148}
66(function wait() { setTimeout(wait, 5000) })();
234function waitFor() {
235 return new Promise(resolve => setTimeout(() => resolve(true), 15));
236}
106export async function wait(timeout: number): Promise<void> {
107 return new Promise<void>((resolve, reject) => setTimeout(resolve, timeout))
108}
49function waitsFor(callback) {
50 if (skipRunsCounter === 0) {
51 jasmineApi.waitsFor.apply(this, arguments);
52 }
53}
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}

Related snippets