Let a promise p implementing a time-based logic similar to an HTTP request, ie. if it is pending for less than 5 seconds and a result has been acquired then resolve otherwise reject with a timeout. I want to use it as a wrapper in an event, so whether anything comes in or not, the promise should be settled anyway. Although the following snippet seems to work, I am not sure that it is implemented correctly:
let p = new Promise(function(resolve, reject) {
evt.on('dataStream', stream => {
if(stream.payload) {
let payload = stream.payload;
resolve(stream)
}
})
setTimeout( () => {
reject('Timeout!')
}, 5000)
});
p.then(res => console.log(res))
.catch(err => console.log(err))