Background
I am trying to make a function that delays the execution of asynchronous functions by X ms.
For the purposes of this demonstration, the following is the async function, which takes an URL:
/*
* This is a simulation of an async function. Be imaginative!
*/
let asyncMock = function(url) {
return new Promise(fulfil => {
setTimeout(() => {
fulfil({
url,
data: "banana"
});
}, 10000);
});
};
Objective
My objective here is to have a function, that will take the url
argument of asyncMock
and then call it every X ms or until there are no more arguments left.
Basically, I want every invocation of asyncMock
to be separated by X ms.
As an example, imagine I call asyncMock
20 times in a row. Normally, those 20 calls would be done immediately. What I want, it to make sure that there is Xms of delay between each of the 20 calls.
Tentative
My idea to solve this, is to have a factory, that will return a promise that will execute the function after X ms.
let throttleFactory = function(args) {
let {
throttleMs
} = args;
let promise = Promise.resolve();
let throttleAsync = function(url) {
return promise.then(() => {
setTimeout(anUrl => {
return new Promise( fulfil => {
fulfil(asyncMock(anUrl));
});
}, throttleMs, url);
});
};
return Object.freeze({
throttleAsync
});
};
Ideally I would use this factory like in the example bellow:
let throttleFuns = throttleFactory({
throttleMs: 2000
});
console.log('running');
throttleFuns.throttleAsync('http://www.bananas.pt')
.then(console.log)
.catch(console.error);
throttleFuns.throttleAsync('http://www.fruits.es')
.then(console.log)
.catch(console.error);
throttleFuns.throttleAsync('http://www.veggies.com')
.then(console.log)
.catch(console.error);
// a ton of other calls in random places in code
Problem
The problem here is that my throttleAsync
fucntion outputs undefined
three times immediately. I believe this might be because I am not defining promise
properly.
Question
How can I fix this code to work as intended?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…