Answer the question
In order to leave comments, you need to log in
How to write an asynchronous function in pure JS for Chrome mobile browser?
In desktop Chrome, the async/await construct works, but not in mobile (the message is not displayed, but without async it is displayed). How to write this function for mobile browser?
window.addEventListener('load', async () => {
alert('привет')
})
Answer the question
In order to leave comments, you need to log in
Indeed , Sergey Sokolov separately this code works on the mobile version of Chrome 70.0.3538.80 with async. In my case, the error may be in another ...
Transpile your code: babel, typescript
Or don't support browsers that don't support the syntax features you need. I don't see any other viable options.
Depends on what version of the browser you are going to run the code on. Arrow functions seem to be included in the ES5 standard, so they should be supported by modern browsers, but async / await constructs are not supported by default in all browsers, so you need babel to make them work everywhere. It's better to use Promises, callbacks, and closures instead. And, in principle, to check some feature, use https://caniuse.com/
jQuery has an analogue of Promise - Deferred objects. If you are not using jQuery, you can find and pull individual functions or find one already on the web.
(function ($) {
'use strict';
void function main() {
// Пример использования promise
promise(function (resolve, reject) {
resolve('example data');
}).done(function (data) {
console.log(data); //> "example data"
});
// Пример использования promiseAll
var deferreds = [$.Deferred().resolve(1), $.Deferred().resolve(2), $.Deferred().resolve(3)];
promiseAll(deferreds).done(function () {
console.log(arguments); //> [1, 2, 3]
});
}();
function promise(callback) {
var deferred = $.Deferred();
callback && callback(deferred.resolve, deferred.reject);
return deferred;
}
function promiseAll(deferreds) {
return $.when.apply(null, deferreds);
}
}(jQuery));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question