| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 // This function will be used only when we need to wait for data gathering. | 7 // This function will be used only when we need to wait for data gathering. |
| 8 function waitDuration(duration) { | 8 function waitDuration(duration) { |
| 9 return new Promise(function(resolve, reject) { | 9 return new Promise(function(resolve, reject) { |
| 10 console.log('Waiting for ', duration.toString(), 'msec'); | 10 console.log('Waiting for ', duration.toString(), 'msec'); |
| 11 setTimeout( | 11 setTimeout( |
| 12 function() { | 12 function() { |
| 13 console.log('Done waiting'); | 13 console.log('Done waiting'); |
| 14 resolve(); | 14 resolve(); |
| 15 }, duration); | 15 }, duration); |
| 16 }); | 16 }); |
| 17 } | 17 } |
| 18 | 18 |
| 19 function waitFor(description, predicate) { | 19 function waitFor(description, predicate) { |
| 20 return new Promise(function(resolve, reject) { | 20 return new Promise(function(resolve, reject) { |
| 21 var startTime = new Date(); | 21 var startTime = new Date(); |
| 22 console.log('Waiting for', description.toString()); | 22 console.log('Waiting for', description.toString()); |
| 23 var check = setInterval(function() { | 23 var check = setInterval(function() { |
| 24 var elapsed = new Date() - startTime; | 24 var elapsed = new Date() - startTime; |
| 25 if (elapsed > 3000) { | 25 if (predicate()) { |
| 26 clearInterval(check); |
| 27 resolve(); |
| 28 } else if (elapsed > 3000) { |
| 26 startTime = new Date(); | 29 startTime = new Date(); |
| 27 console.log('Still waiting for satisfaction of ' + | 30 console.log('Still waiting for satisfaction of ' + |
| 28 predicate.toString()); | 31 predicate.toString()); |
| 29 } else if (predicate()) { | |
| 30 clearInterval(check); | |
| 31 resolve(); | |
| 32 } | 32 } |
| 33 }, 50); | 33 }, 50); |
| 34 }); | 34 }); |
| 35 } | 35 } |
| OLD | NEW |