I have written a function with two parameters:
- parameter 1: a callback function,
- parameter 2: time x (ms)
This function will execute the callback function after a specified period of time x milliseconds and also return a promise.
Below is my code example - it currently only logs 'Goodbye' after 1 second:
function doAfter(func, time) {
var promise = new Promise(function(resolve, reject) {
setTimeout(function(data) {
resolve(data);
}, time);
});
return promise;
}
function sayHello() {
console.log('Hello');
}
function sayGoodbye() {
console.log('Goodbye');
}
doAfter(sayHello, 1000).then(sayGoodbye);
Expected functionality: wait for 1 second, then log 'Hello Goodbye'