5 examples of 'async const function' in JavaScript

Every line of 'async const function' 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
34return function asyncFunction() {
35 const args = Array.from(arguments);
36 return new Promise(function (resolve, reject) {
37 let gen;
38
39 function step(next) {
40 const value = next.value;
41 if (next.done) {
42 return resolve(value);
43 }
44 if (value instanceof Promise) {
45 return value.then(
46 result => step(gen.next(result)),
47 error => {
48 try {
49 step(gen.throw(error));
50 } catch (err) {
51 throw err;
52 }
53 }
54 ).catch((err) => {
55 warn("Unhandled error in async function.", err);
56 reject(err);
57 });
58 }
59 step(gen.next(value));
60 }
61
62 if (typeof func !== "function") {
63 reject(new TypeError("Expected a Function."));
64 }
65 // not a generator, wrap it.
66 if (func.constructor.name !== "GeneratorFunction") {
67 gen = (function*() {
68 return func.call(self, ...args);
69 }());
70 } else {
71 gen = func.call(self, ...args);
72 }
73 try {
74 step(gen.next());
75 } catch (err) {
76 warn("The generator threw immediately.", err);
77 reject(err);
78 }
79 });
80};
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

50function _async (_function) {
51 return function () {
52 var
53 m = this,
54 _arguments = arguments
55 ;
56 setTimeout (function () {_function.apply (m,_arguments)},0);
57 };
58}
149exports.async = function asyncFunction(iter) {
150 return new Promise((resolve, reject) => {
151 resume('next', undefined);
152 function resume(type, value) {
153 try {
154 let result = iter[type](value);
155 if (result.done) {
156 resolve(result.value);
157 } else {
158 Promise.resolve(result.value).then(
159 x => resume('next', x),
160 x => resume('throw', x));
161 }
162 } catch (x) {
163 reject(x);
164 }
165 }
166 });
167};
325function constFunc(func) {
326 Object.freeze(func.prototype);
327 return Object.freeze(func);
328}
143value: function async(callback) {
144 for (var _len4 = arguments.length, args = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
145 args[_key4 - 1] = arguments[_key4];
146 }
147
148 var deferred = new Deferred();
149
150 setTimeout(function () {
151 try {
152 var result = callback.apply(deferred, args);
153 deferred.resolve(result);
154 } catch (err) {
155 deferred.reject(err);
156 }
157 }, 0);
158
159 return deferred;
160}

Related snippets