Last active
June 4, 2019 15:04
-
-
Save d0peCode/147dc2e0c44c8d64386253d563ff6db4 to your computer and use it in GitHub Desktop.
It's node API with /test route. In order to run it you need to get both files, install package.json dependiecies and run cURL / Postman request GET to localhost:5000/test
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
// this is simple 1 file 1 route app; | |
// #bluebird, #bluebird-retry, #bluebird-cancellation, #eventBus | |
// use bluebird retry for 4s; after 1.5s through eventBus invoke bluebird-cancellation which should stop bluebird-retry as well | |
const express = require('express'); | |
const app = express(); | |
const path = require('path'); | |
const bodyParser = require('body-parser'); | |
app.use(bodyParser.json()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
const Promise = require('bluebird'); | |
const retry = require('bluebird-retry'); | |
const eventBus = require('js-event-bus')(); | |
Promise.config({warnings: true, longStackTraces: true, cancellation: true, monitoring: true}); | |
//promise child function | |
const child = () => { | |
return new Promise((resolve, reject, onCancel) => { | |
console.log('CALLED CHILD'); | |
reject(new Error('Rejected from child')); | |
onCancel(() => { | |
console.log('Registered cancel in child function'); | |
}); | |
}); | |
}; | |
//promise parent function | |
const parent = () => { | |
return new Promise((resolve, reject, onCancel) => { | |
setTimeout(async() => { | |
console.log('CALLED PARENT'); | |
try { | |
await retry(child, { max_tries: 4, interval: 500 }) | |
} catch (e) { | |
reject(new Error('Rejected from parent function')); | |
} | |
},100); | |
onCancel(() => { | |
console.log('Registered cancel in parent function'); | |
retry.StopError(new Error('Stop retrying child')); | |
reject(new Error('Cancelled parent')); | |
}); | |
}); | |
}; | |
//background manager - should be able to stop promise chain | |
const backgroundProcess = () => { | |
let index = 0; | |
const intervalId = setInterval(() => { | |
index++; | |
if(index === 3) { | |
console.warn('Emit stop::all event'); | |
eventBus.emit('stop::all'); | |
clearInterval(intervalId); | |
} | |
}, 500); | |
}; | |
//another promise | |
const continueStack = () => { | |
return new Promise((resolve, reject) => { | |
console.log('started continueStack'); | |
setTimeout(() => { | |
console.log('finished continueStack'); | |
resolve(); | |
}, 2000); | |
}); | |
}; | |
//test route here | |
app.get('/test', async(req, res) => { | |
backgroundProcess(); | |
eventBus.on('stop::all', () => { | |
if(promise && promise.cancel) { | |
console.warn('Caught stop::all event'); | |
promise.cancel(); | |
} else { | |
console.log('somehow promise was not properly set at this point'); | |
} | |
}); | |
let promise; | |
try { | |
promise = parent(); | |
await promise; // so continueStack is not called until parent resolves, I guess this is your desire | |
console.log('after parent promise'); | |
await continueStack(); | |
} catch (e) { | |
console.log('Caught error in endpoint'); | |
} | |
res.send('Completed'); | |
}); | |
app.listen(5000, () => { | |
console.log('Serwer nasłuchuje na porcie: ', 5000) | |
}); |
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
{ | |
"name": "bluebirdp", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"bluebird": "^3.5.4", | |
"bluebird-retry": "^0.11.0", | |
"body-parser": "^1.17.2", | |
"express": "^4.15.3", | |
"js-event-bus": "^1.0.0", | |
"nodemon": "^1.11.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment