【发布时间】:2018-09-19 20:43:36
【问题描述】:
我想用 promise 和 async/await 创建串行操作。代码如下。而不是输出:1 2,我有时会得到2 1。有人知道,如何解决我的问题?有可能只用承诺吗?带发电机?也可以是,onResolve() 函数异步工作,所以我也需要使它们同步。
顺序:
request.get(payload, onResolve, onReject) // 先获取
.get(payload, onResolve, onReject) //第二次获取
运行first get() 对有效载荷(_doSomething(payload)) 执行操作,在启动onResolve() 或onReject 后取决于_doSomething() 结果,在onResolve() 或onReject() 启动后second get() 和相同的操作第一个get()。
class Queue {
constructor() {
this.data = [];
}
add(record) {
this.data.unshift(record);
}
remove() {
return this.data.pop();
}
isEmpty() {
return this.data.length === 0;
}
}
class Request {
constructor() {
this._queue = new Queue();
this._isPending = false;
}
_doSomething(payload) {
setTimeout(() => {
console.log(payload);
}, Math.random() * 1000);
}
async _runCycle() {
if (this._queue.isEmpty() || this._isPending) return;
this._isPending = true;
const { urlOne, onResolve, onReject } = this._queue.remove();
await Promise.resolve(this._doSomething(urlOne))
.then(() => onResolve())
.catch(() => onReject());
this._isPending = false;
this._runCycle();
}
get(urlOne, onResolve, onReject) {
this._queue.add({ urlOne, onResolve, onReject });
this._runCycle();
return this;
}
}
const request = new Request();
request
.get('1', ()=>1, ()=>0)
.get('2', ()=>2, ()=>0);
【问题讨论】:
-
您的代码无法运行。
-
@appleapple,抱歉,现在试试
-
@appleapple,是的,我需要先运行 get() _doSomething() => onResolve() 或 onReject(),然后再运行第二个 get()。
-
好的。好吧,您的
Promise在此示例中无效。你能稍微改一下,让我更清楚地理解你的问题吗? -
@appleapple,更新问题,希望现在更清楚
标签: javascript promise async-await generator