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.reject('reject') | |
.finally(() => console.log('finally#1')) | |
.catch(error => console.log('catch#1', error)) | |
.catch(error => console.log('catch#2', error)); | |
//finally#1 | |
//catch#1 reject | |
Promise.resolve('resolve') | |
.then(result=> { console.log('then#1', result); return Promise.resolve('next-step'); }) | |
.finally(() => console.log('finally#1')) |
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.reject('error #1') | |
.then(value => console.log('level-1:', value), error => console.log('level-1-error:', error)) | |
.catch(error => console.log('error in catch:', error)) | |
.finally(() => console.log('finally')) | |
.then(value => console.log('level-2:', value), error => console.log('level-2-error:', value)); | |
// level-1-error: error #1 | |
// finally | |
// level-2: undefined |
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 promise = new Promise(resolve => { | |
setTimeout(()=>resolve('success')); | |
}); | |
const startOperation = () => { console.log('start'); }; | |
const finishOperation = () => { console.log('finish'); }; | |
startOperation(); | |
promise |
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 promise = new Promise(resolve => { | |
setTimeout(()=>resolve('success')); | |
}); | |
const startOperation = () => { console.log('start'); }; | |
const finishOperation = () => { console.log('finish'); }; | |
startOperation(); | |
promise |
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 asyncFoo(i) { | |
return Promise.resolve(i); | |
} | |
async function main() { | |
console.log(`Start`); | |
const start = Date.now() | |
let total = 0 | |
for (let i = 0;i < 550000; i++) { |
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 assertNotThrow = function (cb) { | |
var noError = false; | |
try { | |
cb(); | |
noError = true; | |
} catch (e) {} | |
return noError; | |
} | |
function f() { | |
var foo; |
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 importObject = { | |
imports: { | |
foo: arg => { | |
console.log('Imported value from WASM', arg); | |
} | |
} | |
}; |
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 getResponseSize(url) { | |
const response = await fetch(url); | |
const reader = response.body.getReader(); | |
let total = 0; | |
while (true) { | |
const {done, value} = await reader.read(); | |
if (done) return total; | |
total += value.length; | |
} |
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 asyncThrow = async () => { throw new Error('Rejected-value') }; | |
var reject = async () => Promise.reject('Rejected-value'); | |
var foo = async () => { | |
try { | |
await asyncThrow(); | |
} catch (e) { | |
console.log('Catched error:', e); | |
} | |
try { |
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() { | |
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()); | |
}; |