How to cancel timeout inside of Javascript Promise?
问题 I'm toying with promises in JavaScript and tried to promisify setTimeout function: function timeout(ms) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve('timeout done'); }, ms); }); } var myPromise=timeout(3000); myPromise.then(function(result) { console.log(result); // timeout done }) Fairly straightforward but I was wondering how would I go about canceling my timeout before the promise resolves. timeout returns Promise object hence I loose access to value