Skip to content

Instantly share code, notes, and snippets.

@dapplion
Last active December 3, 2019 21:09
Show Gist options
  • Save dapplion/0b5fc7c473f39cfcdb374f5e86a69bd7 to your computer and use it in GitHub Desktop.
Save dapplion/0b5fc7c473f39cfcdb374f5e86a69bd7 to your computer and use it in GitHub Desktop.
@aragon/apm file that causes the aragonCLI node process to never finish. This patch clears timeout after the Promise.race
/**
* Original code from https://github.com/aragon/apm.js/blob/3d818929ce19410ea6a29e12964b19c08312b7a7/src/utils/timeout-promise.js
*/
function original(promise, ms) {
// Create a promise that rejects in <ms> milliseconds
const timeout = new Promise((resolve, reject) => {
const id = setTimeout(() => {
reject('Timed out in ' + ms + 'ms.')
}, ms)
})
// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout])
}
/**
* Patched aragonApmTimeoutHelper function
*/
function patched(promise, ms) {
// Create a promise that rejects in <ms> milliseconds
let timeoutInstance
const timeout = new Promise((resolve, reject) => {
timeoutInstance = setTimeout(() => {
reject('Timed out in ' + ms + 'ms.')
}, ms)
})
// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]).then(res => {
clearTimeout(timeoutInstance)
return res
})
}
/**
* Util to simulate some async call
*/
async function fakeApi(data) {
await new Promise((resolve, reject) => setTimeout(resolve, 1000))
return data
}
/**
* Test run: comment / uncomment each call and see if
*
* `node <script-path>.js`
*
* finishes or not.
*
* Note that in the aragonCLI context the ms variable has a value of 300000
*/
original(fakeApi('api result'), 300000).then(console.log)
// patched(fakeApi('api result'), 300000).then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment