Skip to content

Commit

Permalink
Async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyzharko committed Dec 13, 2017
1 parent 79228c3 commit e1f95e3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 18 deletions.
38 changes: 38 additions & 0 deletions 2-Babel/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
�������� �������:
��� ������ Babel ��������� ������ � Firefox � IE10


Write your own plugin which will convert all true
values to the false.
Ex.:
var q = true; => var q = false;
if (a==true) => if (a===false)

astexplorer.net



��� ������� 2:
������� ������, ������� ������� ��� ��������� console.log();




�� ��� � ������� 3 ������� - �� 3 ������ ���������� babel, �� 4 ������� ��������� ie10, �� 5 - �������� ������

��� ��� ������ ��� ��� ����� ������ �������





���� ��������� babel ������ 3

������� ie10 - 4

������� ������ 5




������ async/await - 5+++
1 change: 1 addition & 0 deletions 2-Babel/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<title>News API</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="nodeListForEach.js"></script>
<script src="node_modules/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@6/promise.min.js"></script> <!-- https://github.com/stefanpenner/es6-promise -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script> <!-- https://github.com/github/fetch -->
</head>
Expand Down
46 changes: 38 additions & 8 deletions 2-Babel/script-compiled.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
'use strict';

// Async/Await - задание со звёздочкой
var readSourceArray = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return sources;

case 2:
return _context.abrupt('return', _context.sent);

case 3:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));

return function readSourceArray() {
return _ref.apply(this, arguments);
};
}();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var sources = [{
name: 'Google News',
logo: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Google_News_Logo.svg/1200px-Google_News_Logo.svg.png',
Expand All @@ -23,15 +51,17 @@ fetch(req).then(function (response) {});
function loadSources() {
var buttons = document.querySelector('.buttons');

sources.forEach(function (source) {
var button = document.createElement('div');
button.classList.add('source');
button.classList.add(source.link);
button.innerHTML = '\n <img class="source-image" src="' + source.logo + '" alt="' + source.name + '">\n <a href="#"><div class="picture lion"></div></a>';
button.addEventListener('click', function () {
return loadNews(source.link);
readSourceArray().then(function (sources) {
sources.forEach(function (source) {
var button = document.createElement('div');
button.classList.add('source');
button.classList.add(source.link);
button.innerHTML = '\n <img class="source-image" src="' + source.logo + '" alt="' + source.name + '">\n <a href="#"><div class="picture lion"></div></a>';
button.addEventListener('click', function () {
return loadNews(source.link);
});
buttons.appendChild(button);
});
buttons.appendChild(button);
});
}

Expand Down
30 changes: 20 additions & 10 deletions 2-Babel/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,30 @@ fetch(req)
})


// Async/Await - задание со звёздочкой
async function readSourceArray(){
return await sources;
}



function loadSources() {
let buttons = document.querySelector('.buttons');

sources.forEach( source => {
let button = document.createElement('div');
button.classList.add('source');
button.classList.add(source.link);
button.innerHTML = `
<img class="source-image" src="${source.logo}" alt="${source.name}">
<a href="#"><div class="picture lion"></div></a>`;
button.addEventListener('click', () => loadNews(source.link) );
buttons.appendChild(button);
});
readSourceArray().then(
(sources => {
sources.forEach( source => {
let button = document.createElement('div');
button.classList.add('source');
button.classList.add(source.link);
button.innerHTML = `
<img class="source-image" src="${source.logo}" alt="${source.name}">
<a href="#"><div class="picture lion"></div></a>`;
button.addEventListener('click', () => loadNews(source.link) );
buttons.appendChild(button);
});
})
)
}

document.addEventListener("DOMContentLoaded", loadSources);
Expand Down

0 comments on commit e1f95e3

Please sign in to comment.