问题
I've been using promises + async/await for some time now, I thought I was comfortable with them, but this situation really has me stumped. Any advice/guidance is greatly appreciated!
Context: My app is making URI requests, but I have to delay each request by a couple seconds. To make the delay more exact, I set up a queue for making the requests. No problem using callbacks, but I've been at it for a while, and I can't seem to wrap my head around how to pull it off using promises.
Sandboxed the callback code below:
const queue = []
let t;
function addToQueue(params, cb) {
queue.push({params,cb})
_run()
}
function _run() {
if (!t && queue.length) {
const {params,cb} = queue.shift()
_dummyFetch(params).then( data => cb(data) )
_startTimer()
}
}
function _startTimer() {
t = setTimeout( _endTimer, 2000 )
}
function _endTimer() {
t = null
_run()
}
async function _dummyFetch() {}
Sandbox debug:
function seconds() { return Math.round(new Date().getTime()/1000) }
function log(t) { console.log(t + " " + seconds()) }
function logFn(t) { return () => log(t) }
log("init")
addToQueue({}, logFn("request 1")) // should be close/same as init time
addToQueue({}, logFn("request 2"))
addToQueue({}, logFn("request 3"))
// If I could figure out how to make it a promise:
// addToQueue( ... ).then( data => ... )
回答1:
Create a new Promise
in the addToQueue
function and put the resolver function on the queue. Then later resolve the promise with the fetch result:
function addToQueue(params) {
return new Promise(resolve => {
queue.push({params, resolve})
_run()
})
}
function _run() {
if (!t && queue.length) {
const {params, resolve} = queue.shift()
resolve(_dummyFetch(params))
_startTimer()
}
}
Alternatively, you can promisify the entire queue code, and use a promise as the queue itself.
const queue = Promise.resolve();
function addToQueue(params) {
const result = queue.then(() => _dummyFetch(params));
queue = queue.then(timer);
return result;
}
function timer() {
return new Promise(resolve => {
setTimeout(resolve, 2000);
});
}
async function _dummyFetch() {}
You could even make the queue wait for the fetch if it takes longer than 2s, so that two requests never run concurrently, by simply changing
queue = Promise.all([result, queue.then(timer)]).then(res => void res, err => void err);
来源:https://stackoverflow.com/questions/54445803/failing-to-convert-a-callback-queue-to-use-promises