This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var all = items => Promise.all(items); | |
async function asyncRandomNumbers() { | |
const response = await fetch('https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new'); | |
return Number(await response.text()); | |
} | |
async function getUsers(count) { | |
const response = await fetch("https://randomuser.me/api/?results=10", { method: "GET" }); | |
return JSON.parse(await response.text()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getValue = () => { console.log('get-value'); return 'value'; }; | |
async function* foo(value = getValue()) { | |
console.log('before yield', value); | |
yield value + '-yiled'; | |
console.log('after yield'); | |
return value + '-result'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.resolve(1) | |
.then(value => { console.log('fulfill:', value); return 'new value'; }) | |
.finally(value => { console.log('finally:', value); }); | |
// fulfill: 1 | |
// finally: new value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = async function*() {}; | |
typeof foo; | |
// "function" | |
foo.toString(); | |
// "async function* () {}" | |
foo[Symbol.toStringTag]; | |
// "AsyncFunction" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = async function() {}; | |
typeof foo; | |
// "function" | |
foo.toString(); | |
// "async function () {}" | |
foo[Symbol.toStringTag]; | |
// "AsyncFunction" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function* asyncRandomNumbers() { | |
// This is a web service that returns a random number | |
const url = 'https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new'; | |
while (true) { | |
const response = await fetch(url); | |
const text = await response.text(); | |
yield Number(text); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var apiRoot = "https://jsonplaceholder.typicode.com/"; | |
var currentUserCode = "[email protected]"; | |
function fetchCurrentUser(cb) { | |
$.ajax({url: apiRoot + "users", | |
complete: function(result) { | |
if (result.status === 200) { | |
var users = result.responseJSON; | |
var currentUser = users | |
.filter(user => user.email === currentUserCode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo () { | |
try { | |
throw new Error('Error'); | |
} catch (boo) { | |
eval('{ function boo() {} }'); | |
print(boo); // should be Error | |
} | |
print(boo); // should be function boo | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getOrder1(id) { | |
return _http.get('some/path'+1); | |
} | |
async function getOrder2(id) { | |
return _http.get('some/path'+1); | |
} | |
async function flow(id) { | |
const someValue = await getOrder1(id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function foo (value) { | |
return Promise.resolve(value + 1); | |
} | |
async function bar (value) { | |
return Promise.resolve(value + 2); | |
} | |
async function foobar() { | |
const value = await foo(1); |