Skip to content

Instantly share code, notes, and snippets.

@ossareh
Last active August 20, 2018 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ossareh/0cc5a8229d8a2be2f275d3357a6a16f7 to your computer and use it in GitHub Desktop.
Save ossareh/0cc5a8229d8a2be2f275d3357a6a16f7 to your computer and use it in GitHub Desktop.
wut?
#!/usr/bin/env node
const inputs = [false, false, false, true, false, false];
const fast = (i, d) => {
console.log(`run ${d}`);
return i;
};
const slow = async (i) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (i) {
throw new Error(`inside error`);
}
return resolve(i);
}, 3000);
});
};
inputs.forEach(async (i, d) => {
try {
const a = await slow(fast(i, d));
if (a) {
throw new Error('errored');
}
console.log(`${d} done`);
} catch (err) {
console.log(err);
}
});

Expected:

run 0
run 1
run 2
run 3
run 4
run 5
done 0
done 1
done 2
<ERROR>

Actual:

run 0
run 1
run 2
run 3
run 4
run 5
rando.js:13
				throw new Error(`inside error`);
				^

Error: inside error
    at Timeout.setTimeout [as _onTimeout] (rando.js:13:11)
    at ontimeout (timers.js:424:11)
    at tryOnTimeout (timers.js:288:5)
    at listOnTimeout (timers.js:251:5)
    at Timer.processTimers (timers.js:211:10)
#!/usr/bin/env node
const inputs = [false, false, false, true, false, false];
const fast = (i, d) => {
console.log(`run ${d}`);
// pass d to slow
return [i, d];
};
const slow = async ([i, d]) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (i) {
throw new Error(`inside error`);
}
return resolve(i);
// multiply wait time by d
}, 100 * d);
});
};
inputs.forEach(async (i, d) => {
try {
const a = await slow(fast(i, d));
if (a) {
throw new Error('errored');
}
console.log(`${d} done`);
} catch (err) {
console.log(err);
}
});
run 0
run 1
run 2
run 3
run 4
run 5
0 done
1 done
2 done
rando.js:13
				throw new Error(`inside error`);
				^

Error: inside error
    at Timeout.setTimeout [as _onTimeout] (rando.js:13:11)
    at ontimeout (timers.js:424:11)
    at tryOnTimeout (timers.js:288:5)
    at listOnTimeout (timers.js:251:5)
    at Timer.processTimers (timers.js:211:10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment