// ES6 code, without async/await function httpGet(url) { return new Promise(function (resolve, reject) { // do the usual Http request var request = new XMLHttpRequest(); request.open('GET', url); request.onload = function () { if (request.status == 200) { resolve(request.response); } else { reject(Error(request.statusText)); } }; request.onerror = function () { reject(Error('Network Error')); }; request.send(); }); } function httpGetJson(url) { return new Promise(function (resolve, reject) { // check if the URL looks like a JSON file and call httpGet. var regex = /\.(json)$/i; if (regex.test(url)) { // call the promise, wait for the result resolve(httpGet(url).then(function (response) { return response; }, function (error) { reject(error); })); } else { reject(Error('Bad File Format')); } }); } httpGetJson('file.json').then(function (response) { console.log(response); }).catch(function (error) { console.log(error); });
// ES7 code, with async/await function httpGet(url) { return new Promise(function (resolve, reject) { // do the usual Http request let request = new XMLHttpRequest(); request.open('GET', url); request.onload = function () { if (request.status == 200) { resolve(request.response); } else { reject(Error(request.statusText)); } }; request.onerror = function () { reject(Error('Network Error')); }; request.send(); }); } async function httpGetJson(url) { // check if the URL looks like a JSON file and call httpGet. let regex = /\.(json)$/i; if (regex.test(url)) { // call the async function, wait for the result return await httpGet(url); } else { throw Error('Bad Url Format'); } } httpGetJson('file.json').then(function (response) { console.log(response); }).catch(function (error) { console.log(error); });
// call the async function, wait for the result let func = async () => await httpGet(url); return await func();
function spawn(genF, self) { return new Promise(function (resolve, reject) { var gen = genF.call(self); function step(nextF) { var next; try { next = nextF(); } catch (e) { // finished with failure, reject the promise reject(e); return; } if (next.done) { // finished with success, resolve the promise resolve(next.value); return; } // not finished, chain off the yielded promise and `step` again Promise.resolve(next.value).then(function (v) { step(function () { return gen.next(v); }); }, function (e) { step(function () { return gen.throw(e); }); }); } step(function () { return gen.next(undefined); }); }); }
function httpGetJson(url) { return spawn(function* () { // check if the URL looks like a JSON file and call httpGet. var regex = /\.(json)$/i; if (regex.test(url)) { // call the async function, wait for the result return yield httpGet(url); } else { throw Error('Bad Url Format'); } }, this); }
// original JavaScript code function foo(argument = true) { // some stuff } // representation of the Bytecode Generator's output in JavaScript function foo(argument) { argument = true; // some stuff }
// representation of the Bytecode Generator's output in JavaScript function foo(argument) { try { argument = true; } catch (error) { return Promise.reject(error); } return spawn(function* () { // keep this call as we are in an async function // some stuff }, this); }
Source: https://habr.com/ru/post/269871/
All Articles