Async/Await

In this example below you will see how to do a Async/Await with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by ChrisRast, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright ChrisRast ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Async/Await</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
  
  
</head>

<body>

  
  <script src='https://raw.githubusercontent.com/facebook/regenerator/master/packages/regenerator-runtime/runtime.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.29/browser-polyfill.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/ChrisRast/asyncawait-YQQeGg */
"use strict";

var getAPI = function () {
	var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
		var response;
		return regeneratorRuntime.wrap(function _callee$(_context) {
			while (1) {
				switch (_context.prev = _context.next) {
					case 0:
						_context.prev = 0;
						_context.next = 3;
						return fetch('http://ip.jsontest.com').json();

					case 3:
						response = _context.sent;

						console.log(response);
						return _context.abrupt("return", response);

					case 8:
						_context.prev = 8;
						_context.t0 = _context["catch"](0);

						console.warn(_context.t0);

					case 11:
					case "end":
						return _context.stop();
				}
			}
		}, _callee, this, [[0, 8]]);
	}));

	return function getAPI() {
		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"); }); }; }

getAPI();

Comments