Thanks Ryan! That is a very elegant timeout implementation, just add .timeout(ms, msg). And has many other great features, such as cancellation and delays.
I assumed that native implementation will always be faster & safer to use long-term, thought that 3rd party libs were just a placeholder while native APIs are in development. It appears that I was I was very wrong about speed at least: http://programmers.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird And seeing how I am implementing features that bluebird already has, the solution seems pretty clear - will switch to bluebird. Alen On Thursday, June 25, 2015 at 5:55:24 PM UTC-7, Ryan Graham wrote: > > Bluebird appears to have timeouts: > > > https://github.com/petkaantonov/bluebird/blob/master/API.md#timeoutint-ms--string-message---promise > > ~Ryan > > On Thu, 25 Jun 2015 at 14:06 Gustavo Machado <[email protected] > <javascript:>> wrote: > >> Hi Alen, >> >> Check out the "race" method of promises, might help simplify this a lot. >> >> >> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race >> >> Cheers, >> Gus >> >> On Thu, Jun 25, 2015 at 3:06 PM, <[email protected] <javascript:>> wrote: >> >>> Hello, >>> >>> Being relatively new to promises, a few months into using them (the >>> native implementation on nodejs) I found myself adding timeout rejections >>> repeatedly. For me it is a very common use case. >>> >>> I took a quick look and didn't find Promises with built in timeouts. The >>> code below is my wrapper to handle that, it seems to work fine & passes my >>> tests. The 2 questions I have are (1) is there something that's commonly >>> used for this that I missed in my search and (2) if not - does the wrapper >>> seem alright or is there a potential problem? >>> >>> Thank you! >>> Alen >>> >>> var DEFAULT_TIMEOUT = 5000; >>> >>> var TimedPromise = function(timeoutAfter) { >>> >>> var _self = this; >>> >>> var _rejectFunc; >>> var _resolveFunc; >>> >>> var _timeoutMs = timeoutAfter ? timeoutAfter : DEFAULT_TIMEOUT; >>> >>> var _promise = new Promise(function(resolveFunc, rejectFunc) { >>> _resolveFunc = resolveFunc.bind(this); >>> _rejectFunc = rejectFunc.bind(this); >>> }); >>> >>> var _timeout = setTimeout(function() { >>> >>> if (_rejectFunc) { >>> _rejectFunc(Error('timeout')); >>> } >>> else { >>> >>> _promise.then = function() { >>> return Promise.reject(new Error('timeout')); >>> }; >>> } >>> }, >>> _timeoutMs >>> ); >>> >>> _self.resolve = function(arg) { >>> >>> if (_timeout) { >>> clearTimeout(_timeout); >>> } >>> >>> _resolveFunc(arg); >>> }; >>> >>> _self.reject = function(arg) { >>> >>> if (_timeout) { >>> clearTimeout(_timeout); >>> } >>> >>> _rejectFunc(arg); >>> }; >>> >>> _self.promise = function() { >>> return _promise; >>> }; >>> >>> }; >>> >>> exports = module.exports = TimedPromise; >>> >>> >>> >>> -- >>> Job board: http://jobs.nodejs.org/ >>> New group rules: >>> https://gist.github.com/othiym23/9886289#file-moderation-policy-md >>> Old group rules: >>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "nodejs" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected] <javascript:>. >>> To post to this group, send email to [email protected] >>> <javascript:>. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/nodejs/4c1b478c-b2ff-453d-b35d-d5a4de967af2%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/nodejs/4c1b478c-b2ff-453d-b35d-d5a4de967af2%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> Job board: http://jobs.nodejs.org/ >> New group rules: >> https://gist.github.com/othiym23/9886289#file-moderation-policy-md >> Old group rules: >> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >> --- >> You received this message because you are subscribed to the Google Groups >> "nodejs" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/nodejs/CAJkwh4Kg%3D3HayO_Dkh1SHSpa2SncY7JN4QzuKMacJBHPBXgE5Q%40mail.gmail.com >> >> <https://groups.google.com/d/msgid/nodejs/CAJkwh4Kg%3D3HayO_Dkh1SHSpa2SncY7JN4QzuKMacJBHPBXgE5Q%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/18ae37ee-4933-4c6d-8ef0-dc1b82d99f8e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
