Callbacks: function checkConnection(cb) { var img = document.createElement('img'); img.onload = function () { return cb(true); }; img.onerror = function () { return cb(false); }; img.src = "https://still-forest-89345.herokuapp.com/java_developer.jpg"; } checkConnection(function (connected) { if (connected) { alert("Connected"); } else { alert("Not connected"); });
Promises: function checkConnection() { return new Promise(function (resolve, reject) { var img = document.createElement('img'); img.onload = resolve; img.onerror = reject; img.src = "https://still-forest-89345.herokuapp.com/java_developer.jpg"; }); } checkConnection().then(function () { alert("Connected"); }, function () { alert("Not connected"); });
Обсуждают сегодня