D
D
Dimka52018-11-14 12:32:22
JavaScript
Dimka5, 2018-11-14 12:32:22

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

4 answer(s)
D
Dimka5, 2018-11-15
@Dimka5

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 ...

I
Interface, 2018-11-14
@Interface

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.

M
Maxim Yakovenko, 2018-11-14
@TheLazzziest

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/

I
Ihor Bratukh, 2018-11-14
@BRAGA96

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 question

Ask a Question

731 491 924 answers to any question