This file contains 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
rem Forcefully terminates all node processes | |
taskkill /f /im node.exe |
This file contains 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 makeRequests(urls){ | |
let requests = urls.map(url => xhr({url: url, responseType: 'arraybuffer'})); | |
all(requests) | |
.then(response => { | |
response.forEach(value => { | |
if (value instanceof Error){ | |
console.error(value); | |
} | |
This file contains 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 all(promises){ | |
let len = promises.length, | |
returned = 0, | |
responses = []; | |
let totalPromise = new Promise((resolve, reject) => { | |
promises.forEach((p, i) => { | |
p.then(response => { | |
responses[i] = response; | |
}) |
This file contains 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 xhr({method = 'GET', url, async = true, responseType = ''}){ | |
let req = new XMLHttpRequest(); | |
req.open(method, url, async); | |
req.responseType = responseType; | |
let p = new Promise((resolve, reject) => { | |
req.onreadystatechange = () => { | |
if (req.readyState == XMLHttpRequest.DONE){ | |
if (200 <= req.status && req.status < 300){ | |
resolve(req.response); |
This file contains 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 a = () => Promise.resolve(1); | |
var b = () => Promise.reject(new Error(2)); | |
var c = () => Promise.resolve(3); | |
Promise.all([a(), b(), c()].map(p => p.catch(e => e))) | |
.then(results => console.log(results)) // 1,Error: 2,3 | |
.catch(e => console.log(e)); |
This file contains 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 allFulfilled(answerPs) { | |
let countDown = answerPs.length; | |
const answers = []; | |
if (countDown === 0) { return answers; } | |
const deferredResult = Q.defer(); | |
answerPs.forEach(function(answerP, index) { | |
Q(answerP).when(function(answer) { | |
answers[index] = answer; | |
if (--countDown === 0) { deferredResult.resolve(answers); } | |
}, function(err) { |
This file contains 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 limiter(fn, wait){ | |
let isCalled = false; | |
return function(){ | |
if (!isCalled){ | |
fn(); | |
isCalled = true; | |
setTimeout(function(){ | |
isCalled = false; | |
}, wait) |
This file contains 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
// Broken Code | |
function limiter(fn, wait){ | |
let isCalled = false, | |
calls = []; | |
return function(){ | |
calls.push(fn); | |
// Infinite Loop | |
while (calls.length){ |
This file contains 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 limiter(fn, wait){ | |
let isCalled = false, | |
calls = []; | |
let caller = function(){ | |
if (calls.length && !isCalled){ | |
isCalled = true; | |
calls.shift().call(); | |
setTimeout(function(){ | |
isCalled = false; |
This file contains 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 limiter(fn, wait){ | |
let isCalled = false, | |
calls = []; | |
let caller = function(){ | |
if (calls.length && !isCalled){ | |
isCalled = true; | |
calls.shift().call(); | |
setTimeout(function(){ | |
isCalled = false; |
OlderNewer